【发布时间】:2011-05-25 15:13:24
【问题描述】:
我知道我可以像这样对列表进行排序
var result = List<T>.OrderBy(value => value.property);
但是假设我有这样的东西
class Stock
{
public Guid ID { get; set; }
public string Description { get; set; }
}
class StockList : List<Stock>
{
public enum SomeEnum
{
SomeOtherValue = 0,
SomeOtherOtherValue
}
//What if I want method like this using LINQ?
public void Sort(SomeEnum sortBy)
{
switch (sortBy)
{
case SomeValue.SomeOtherOtherValue:
//Sort one way
break;
case SomeValue.SomeOtherValue:
//Sort another
break;
}
}
}
this.OrderBy()(我假设其他 LINQ 扩展)返回一个 OrderedCollection 并且似乎不会影响原件。所以我认为我的做法是错误的?
【问题讨论】:
-
请注意,当您执行
List<T>.OrderBY(...)时,您并未对列表进行排序,而是返回了一个可按顺序枚举列表的可枚举。即列表不受影响(如您所见)。
标签: c# linq sorting collections ienumerable