我有一張產品規格表,我不想在顯示產品規格時顯示重復的元素。我這樣做了,但顯示錯誤。我哪里做錯了?
并顯示此錯誤:“IEnumerable”不包含定義
private static List<ProductPropertiesQueryModel>
MapProductProperties(List<ProductProperties> ProductProperties)
{
return ProductProperties
.Select(x=>new ProductPropertiesQueryModel
{
ProductId =x.ProductId,
Color =x.Color,
Size=x.Size
}).DistinctBy(r=>r.Color).ToList();
}
uj5u.com熱心網友回復:
DistinctBy 附帶 .NET 6(新的 LINQ API部分)。看起來您使用的是較低版本。您可以嘗試以下其中一種。
1-您可以自己撰寫擴展程式,
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> listItems, Func<T, TKey> groupingKey)
{
return listItems.GroupBy(groupingKey).Select(x => x.First());
}
2-升級您的 .NET 版本,
3-GroupBy()直接使用,
private static List<ProductPropertiesQueryModel> MapProductProperties(List<ProductProperties> ProductProperties)
{
return ProductProperties
.Select(x=>new ProductPropertiesQueryModel
{
ProductId =x.ProductId,
Color =x.Color,
Size=x.Size
}).GroupBy(x => x.Color).Select(x => x.First()).ToList();
}
4- 使用MoreLinq擁有DistinctBy()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452682.html
標籤:asp.net-mvc asp.net 核心
上一篇:有沒有辦法在ASP.NETMVC中將資料添加到多對多關系的鏈接表中?
下一篇:通過外部網頁處理請求
