RavenDB OrderByDescending并采用-错误的结果 [英] RavenDB OrderByDescending and Take - Incorrect Results

查看:134
本文介绍了RavenDB OrderByDescending并采用-错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近,该查询才对我有用.现在,我的RavenDB中有135个InstallationSummary文档.它不是按开始时间获取最新的文件,而是有效的 ,但是最后一对,最新的文档没有从该查询中显示.我查询不正确吗?我应该知道有没有其他方法可以对RavenDB执行OrderByDescending和Take?我可以正确查询的文件数量是否有限制?

This query was working for me until recently. I now have 135 InstallationSummary documents in my RavenDB. Instead of getting the most recent by start time, it's mostly working, but the last couple, most recent documents aren't showing up from this query. Am I querying incorrectly? Is there a different way to do OrderByDescending and Take with RavenDB that I should be aware of? Is there a document number limit to what I can query correctly?

注意:我已经调试好了,查询确实返回了我们在网格中看到的内容.在运行查询的时间与用户界面中显示的内容之间没有任何转换.

Note: I have debugged this, and the query indeed returns what we see in the grid. There is no transformation done between the time the query is run and what is shown in the UI.

IEnumerable<InstallationSummary> installationSummaries =
  QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
  .Include(x => x.ApplicationServerId)
  .Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
  .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
  .OrderByDescending(summary => summary.InstallationStart)
  .Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();

此网格应在其中显示更多行,且开始时间应大于1/19/2012 6:33:51 PM:

This grid should show a few more rows in it with start times greater than 1/19/2012 6:33:51 PM:

我从查询中删除了Take(numberToRetrieve),在总共160个InstallationSummary文档中,我只得到128个.我可以在RavenDB Studio中看到所有160个,但是查询中只有128个返回. 128 ... 128 ... 2的幂...我达到极限了吗?

I removed Take(numberToRetrieve) from the query, and I'm only getting 128 of the total 160 InstallationSummary documents. I can see all 160 in RavenDB Studio, but only 128 return from the query. 128... 128... power of 2... Did I hit some limit?

好吧,看来我确实达到了128的限制: http://www.blogcoward.com/archive/2010/05/21/RavenDB-and-a-brief-design-philosophy-discussion-with-Ayende.aspx http://codeofrob.com/archive/2010/05/12/ravendb-basic-usage-considerations.aspx

Okay, it looks like I did hit the limit of 128: http://www.blogcoward.com/archive/2010/05/21/RavenDB-and-a-brief-design-philosophy-discussion-with-Ayende.aspx http://codeofrob.com/archive/2010/05/12/ravendb-basic-usage-considerations.aspx

但是为什么呢?我那里有一个Take()方法.我应该如何获取最近的50个文档?

But why? I have a Take() method in there. How am I supposed to get the 50 most recent documents?

有点骇人听闻,下面的查询至少会显示最新的查询.这并不是我想要的,因为无论日期如何,我都想要最近的50个.自开始日期以来最多不超过50个,这至少会显示最新的项目.

As a bit of a hack, the query below will at least show the most recent. It isn't exactly what I want, because I want the most recent 50, regardless of date. As long as there aren't more than 50 since the start date, this will at least show the most recent items.

using Raven.Client.Linq;

DateTime startDate = new DateTime(2012, 1, 18);

IEnumerable<InstallationSummary> installationSummaries =
QueryAndCacheEtags(session => session.Query<InstallationSummary>()
.Include(x => x.ApplicationServerId)
.Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
.Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)                        
.Where(x => x.InstallationStart > startDate)
.OrderByDescending(summary => summary.InstallationStart)                        
.Take(numberToRetrieve)
).Cast<InstallationSummary>().ToList();

我必须从LuceneQuery转到Query,并且必须添加Where子句.

I had to go from a LuceneQuery to just Query and I had to add the Where clause.

推荐答案

最终解决了真正的问题.

Finally worked out the real issue.

IEnumerable<InstallationSummary> installationSummaries =
    QueryAndCacheEtags(session => session.Query<InstallationSummary>()
       .Include(x => x.ApplicationServerId)
       .Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
       .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)                        
       .Where(x => x.InstallationStart > startDate)
       .OrderByDescending(summary => summary.InstallationStart)                        
       .Take(numberToRetrieve))
       .Cast<InstallationSummary>()
       .ToList();

QueryAndCacheEtags(..)函数的签名为Func<T>,而不是Expression<Func<T>>.然后返回IEnumerable<T>而不是IQueryable<T>

The signature of the QueryAndCacheEtags(..) function is Func<T>, not Expression<Func<T>>. And it returns IEnumerable<T> not IQueryable<T>

这会将语句从IQueryable<T>转换为IEnumerable<T>.这意味着RavenDB服务器仅处理查询的第一部分,没有过滤或排序.

This converts the statement from IQueryable<T> to IEnumerable<T> at that point. This means that the RavenDB server only processes the first part of the query, which has no filtering or ordering.

其余的语句,然后在内存中应用于您返回的128个项目.因此,为什么看不到已正确排序或过滤的商品.

The rest of the statements and then applied in-memory to the 128 items you get back. Hence why you aren't seeing the item ordered or filtered properly.

此处此处

通常,您不必担心Func<T>Expression<Func<T>>之间的差异,编译器会为您处理.但是,如果将自己的函数调用引入LINQ语句,则确实需要正确处理.

Generally you don't have to worry about the difference between Func<T> and Expression<Func<T>>, the compiler handles it for you. But if you introduce your own function call into a LINQ statement, you do need to get it right.

这篇关于RavenDB OrderByDescending并采用-错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆