在LINQ,做预测过的IOrderedEnumerable< T> preserve的顺序? [英] In LINQ, do projections off an IOrderedEnumerable<T> preserve the order?

查看:472
本文介绍了在LINQ,做预测过的IOrderedEnumerable< T> preserve的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 IOrderedEnumberable<车> ,我对它进行排序,然后做一个突出的查询......

If I have an IOrderedEnumberable<Car>, I sort it and then do a projecting query...

是为了preserved在投影?

例如,是否这种情况下工作?

For example, does this scenario work?

IOrderedEnumberable<Car> allCarsOrderedFastestToSlowest = 
            GetAllCars()
                  .OrderByDescending(car=>car.TopSpeed);

var top3FastestCarManufacturers =
            allCarsOrderedFastestToSlowest
                  .Select(car=>car.Manufacturer)
                  .Distinct()
                  .Take(3);

请问 top3FastestCarManufacturers 变量的名字传达出什么真正发生在code是什么意思?

Does the name of the top3FastestCarManufacturers variable convey the meaning of what has really happened in the code?

推荐答案

本文档中的分明方法并没有说明该订单是否为preserved什么或不。这可能是因为它取决于源的基础实现

The documentation for the Distinct method doesn't say anything about whether the order is preserved or not. This is probably because it depends on the underlying implementation of the source.

您可以使用分组来获得期望的结果,从各厂家获得最快的车,然后得到最快的三个来自:

You can use grouping to get the desired result, by getting the fastest car from each manufacturer, and then get the three fastest from that:

var topThreeFastestCarManufacturers =
  GetAllCars()
  .GroupBy(c => c.Manufacturer)
  .Select(g => g.OrderByDescending(c => c.TopSpeed).First())
  .OrderByDescending(c => c.TopSpeed)
  .Take(3);

这篇关于在LINQ,做预测过的IOrderedEnumerable&LT; T&GT; preserve的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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