考虑此示例代码
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");
IEnumerable<string> query = fruits.Cast<string>()
.OrderBy(fruit => fruit)
.Where(fruit => fruit.StartsWith("m"))
.Select(fruit => fruit);
我有两个问题:
如果
Select
自身返回相同类型,是否需要编写最后一个Where
子句?该示例来自msdn,为什么他们总是写它?这些方法的正确顺序是什么?订单会影响某些东西吗?如果我交换
Select
和Where
或OrderBy
怎么办? 最佳答案
不,如果您实际上没有转换返回的类型,则Select
不必要。
在这种情况下,方法调用的顺序可能会影响性能。确保在过滤之前对所有对象进行排序比过滤然后对较小的数据集进行排序要花费更长的时间。