在 LINQ 谓词中使用 OrderBy?

发布于 2024-10-31 14:23:18 字数 947 浏览 5 评论 0原文

在我的代码中,我需要按 PriceRating.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

究竟谁懂我的在乎 2024-11-07 14:23:18

您可以轻松地利用 LINQ 的延迟特性以及轻松编写查询的能力。

可能使用这样的代码:

        var baseQuery = from co in collection where !co.IsVirtual select co; // base of query
        IOrderedEnumerable<Result> orderedQuery; // result of first ordering, must be of this type, so we are able to call ThenBy

        switch(CurrentDisplayMode) // use enum here
        { // primary ordering based on enum
            case CRSChartRankingGraphDisplayMode.Position: orderedQuery = baseQuery.OrderBy(co => co.Price);
                break;
            case CRSChartRankingGraphDisplayMode.Grade: orderedQuery = baseQuery.OrderBy(co => co.TotalGrade);
                break;
        }

        this.collectionCompleteSorted = orderedQuery.ThenBy(co => co.CurrentRanking).ToList(); // secondary ordering and conversion to list

它很容易理解,并且直到最后才避免转换为列表。

You can easily make use of deffered nature of LINQ and ability to easily compose queries.

Probably using code like this:

        var baseQuery = from co in collection where !co.IsVirtual select co; // base of query
        IOrderedEnumerable<Result> orderedQuery; // result of first ordering, must be of this type, so we are able to call ThenBy

        switch(CurrentDisplayMode) // use enum here
        { // primary ordering based on enum
            case CRSChartRankingGraphDisplayMode.Position: orderedQuery = baseQuery.OrderBy(co => co.Price);
                break;
            case CRSChartRankingGraphDisplayMode.Grade: orderedQuery = baseQuery.OrderBy(co => co.TotalGrade);
                break;
        }

        this.collectionCompleteSorted = orderedQuery.ThenBy(co => co.CurrentRanking).ToList(); // secondary ordering and conversion to list

Its easy to understand and avoids converting to list until the very end.

无力看清 2024-11-07 14:23:18

如果只是您的排序不同,则将结果返回到 this.collectionCompleteSorted,然后在枚举数据时执行 this.collectionCompleteSorted.OrderBy() 。

this.collectionCompleteSorted = new List<Result>(from co in collection 
                                                             where co.IsVirtual == false                                                                  
                                                             select co);

foreach (var obj in this.collectionCompleteSorted.OrderBy(c => c.Price).ToList())
{
    // do something
}

并通过删除当前的 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.

this.collectionCompleteSorted = new List<Result>(from co in collection 
                                                             where co.IsVirtual == false                                                                  
                                                             select co);

foreach (var obj in this.collectionCompleteSorted.OrderBy(c => c.Price).ToList())
{
    // do something
}

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

写给空气的情书 2024-11-07 14:23:18

为什么不直接获取值(减去排序),然后(正如它所显示的那样)使用案例来对结果进行排序?

// build your collection first
var items = from co in collection
            where !co.IsVirtual
            select co;

// go through your sort selectors
select (CurrentDisplayMode)
{
  case CRSChartRankingGraphDisplayMode.Position:
    this.collectionCompleteSorted = items.OrderBy(i => i.Price).ThenBy(j => j.CurrentRanking).ToList();
    break;
  case CRSChartRankingGraphDisplayMode.Grade:
    this.collectionCompleteSorted = items.OrderBy(i => i.TotalGrade).ThenBy(j => j.CurrentRanking).ToList();
    break;
  ...
  //default: // maybe you want this, too
}

Why not just grab the values (less the sort), then (as it appears) use a case to order the results?

// build your collection first
var items = from co in collection
            where !co.IsVirtual
            select co;

// go through your sort selectors
select (CurrentDisplayMode)
{
  case CRSChartRankingGraphDisplayMode.Position:
    this.collectionCompleteSorted = items.OrderBy(i => i.Price).ThenBy(j => j.CurrentRanking).ToList();
    break;
  case CRSChartRankingGraphDisplayMode.Grade:
    this.collectionCompleteSorted = items.OrderBy(i => i.TotalGrade).ThenBy(j => j.CurrentRanking).ToList();
    break;
  ...
  //default: // maybe you want this, too
}
丢了幸福的猪 2024-11-07 14:23:18
var q = collection.Where(co => !co.IsVirtual);

if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Position)
{
    q = q.OrderBy(co => co.Price).ThenBy(co => co.CurrentRanking);
}
else if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Grade)
{
    q = q.OrderBy(co => co.Rating.TotalGrade).ThenBy(co => co.CurrentRanking);
}

this.collectionCompleteSorted = q.ToList();
var q = collection.Where(co => !co.IsVirtual);

if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Position)
{
    q = q.OrderBy(co => co.Price).ThenBy(co => co.CurrentRanking);
}
else if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Grade)
{
    q = q.OrderBy(co => co.Rating.TotalGrade).ThenBy(co => co.CurrentRanking);
}

this.collectionCompleteSorted = q.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文