在 LINQ 谓词中使用 OrderBy?
在我的代码中,我需要按 Price 或 Rating.TotalGrade 对集合进行排序,正如您所看到的,两个 LINQ 查询几乎是相同的语句,只有细微的差别。
我正在考虑使用 LINQ 谓词来代替,但正如您所看到的,orderby 是主要区别,并且我在查询中没有发现使用 orderby 的示例。是否有可能或者有其他方法来缩短我的代码,也许将来会有更多的情况。
if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Position)
{
this.collectionCompleteSorted = new List<Result>(from co in collection
where co.IsVirtual == false
orderby co.Price, co.CurrentRanking
select co);
}
else if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Grade)
{
this.collectionCompleteSorted = new List<Result>(from co in collection
where co.IsVirtual == false
orderby co.Rating.TotalGrade, co.CurrentRanking
select co);
}
In my code I need to sort a collection either by Price or by Rating.TotalGrade and as you can see both LINQ querys are almost the same statement with only a minor difference.
I was thinking about using a LINQ predicate instead but as you can see the the orderby is the main difference and I found no sample using orderby in a query. Is it possible or are there other ways to shorten my code, Maybe there will be even more conditions in the future.
if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Position)
{
this.collectionCompleteSorted = new List<Result>(from co in collection
where co.IsVirtual == false
orderby co.Price, co.CurrentRanking
select co);
}
else if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Grade)
{
this.collectionCompleteSorted = new List<Result>(from co in collection
where co.IsVirtual == false
orderby co.Rating.TotalGrade, co.CurrentRanking
select co);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以轻松地利用 LINQ 的延迟特性以及轻松编写查询的能力。
可能使用这样的代码:
它很容易理解,并且直到最后才避免转换为列表。
You can easily make use of deffered nature of LINQ and ability to easily compose queries.
Probably using code like this:
Its easy to understand and avoids converting to list until the very end.
如果只是您的排序不同,则将结果返回到 this.collectionCompleteSorted,然后在枚举数据时执行 this.collectionCompleteSorted.OrderBy() 。
并通过删除当前的 linq 查询来删除订单。
如果查询只执行一次,那么您可以在上面示例中的 linq 查询中省略 .ToList() 。当调用 ToList 时,这会导致立即执行查询,如果在稍后对集合的调用中实现了 OrderBy 并且也实现了 ToList,则查询实际上会使用 order by 语句在数据库服务器上执行,将排序从代码卸载到数据库。请参阅 http://blogs.msdn.com/ b/charlie/archive/2007/12/09/deferred-execution.aspx
If just your order by is different, than return your result into this.collectionCompleteSorted, and then do this.collectionCompleteSorted.OrderBy() when you enumerate through the data.
And remove the order by remove your current linq query.
If the query is just being executed once, than you can leave off the .ToList() from the linq query in the example above. When ToList is called, this causes the query to be executed immediately, where if the OrderBy is implemented in a later call to the collection and a ToList is also, than the query would actually be executed on the database server with an order by statement, offloading the ordering from the code to the database. See http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx
为什么不直接获取值(减去排序),然后(正如它所显示的那样)使用案例来对结果进行排序?
Why not just grab the values (less the sort), then (as it appears) use a case to order the results?