1

我正在对 RavenDb 中的数据进行大量后端数据操作(而不是网站 crud)。我有几十种方法散布在各处,用于执行基本相同的跳过/获取查询,但参数不同。我想要的大部分内容很简单:数据库中与此查询匹配的每个文档,执行此操作。

我创建了一个小帮手,但似乎无法弄清楚如何将选择器应用于它。例如,我的最终目标可能是写:

MyDocStore.ForEach<Users>( 
     x => x.Where(u => u.LastName == "Smith").OrderBy( u => u.FirstName), 
     x => Console.WriteLine( "{0}", x.FirstName),
     take: 12);

这是我到目前为止所拥有的:

public static class RavenHelper
{
    public static void ForEach<TSource>(
         this IDocumentStore documentStore, 
          Func<TSource, IEnumerable<TSource>> selector, 
          Action<TSource> action, 
          int take = 128)
    {
        var skip = 0;
        while( true )
        {
            using( var session = documentStore.OpenSession() )
            {
                var list = session.Query<TSource>();
                // How do I apply more selectors? 
                var result = list.Take( take ).Skip( skip ).ToList();

                if( !result.Any() )
                {
                    return;
                }

                foreach( var il in result )
                {
                    action(il);
                }

                skip += take;
            }
        }
    }
}
4

1 回答 1

2

你需要:

      Expression<Func<TSource, bool>> selector, 

然后调用:

       .Where(selector)
于 2012-04-11T05:40:59.670 回答