我有了這個LINQ從一個泛型列表獲取特定類成員的不同值:如何在LINQ(C#)中對不同的val進行排序?
var distinctDescriptions = itemsForMonthYearList.Select(x => x.ItemDescription).Distinct();
的泛型列表的定義是這樣的:
List<ItemsForMonthYear> itemsForMonthYearList;
類是:
public class ItemsForMonthYear
{
public String ItemDescription { get; set; }
public String monthYr { get; set; }
public int TotalPackages { get; set; }
public Decimal TotalPurchases { get; set; }
public Decimal AveragePrice { get; set; }
public Double PercentOfTotal { get; set; }
}
我想這會工作:
var distinctDescriptions = itemsForMonthYearList.Select(x => x.ItemDescription).Distinct().OrderBy(x => x.ItemDescription);
...但它甚至不編譯:
「‘串’不包含‘ItemDescription’,沒有 擴展方法的定義‘ItemDescription’接受 類型的第一個參數」字符串」可以找到(是否缺少using指令或程序 集引用?)「
我怎樣才能在不同的值按字母順序排序?