我有一个Product类的列表,我想按计算出的价格对其进行排序。
public class Product
{
public decimal Price { get; set; }
public int Amount { get; set; }
public decimal CalculatePrice
{
get
{
return Price * Amount;
}
}
}
我已经尝试了以下方法,但是不起作用:
products.OrderBy(p => p.CalculatePrice);
List<T>
s member methods confused:.OrderBy
is a LINQ extension method that returns a lazily-evaluatedIEnumerable<T>
that represents a sorted version of the originalList<T>
, however it does not perform any immediate sorting nor alter/change ("mutate")..OrderBy
uses aFunc<T,TSortBy>
(orExpression<Func<T,TSortBy>>
) to choose a single property to compare.List<T>
has a methodSort
which re-orders theList<T>
in-place without returning a new list or new view of the same list..Sort
uses aFunc<T,T,Int32>
as a custom comparator.