我有了这个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指令或程序 集引用?)“
我怎样才能在不同的值按字母顺序排序?