LINQ不同或多个属性组 [英] linq distinct or group by multiple properties
本文介绍了LINQ不同或多个属性组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用C#和LINQ获得结果
从下表:
How can I using c# and Linq to get a result
from the next list:
var pr = new List<Product>()
{
new Product() {Title="Boots",Color="Red", Price=1},
new Product() {Title="Boots",Color="Green", Price=1},
new Product() {Title="Boots",Color="Black", Price=2},
new Product() {Title="Sword",Color="Gray", Price=2},
new Product() {Title="Sword",Color="Green",Price=2}
};
结果
:
{Title="Boots",Color="Red", Price=1},
{Title="Boots",Color="Black", Price=2},
{Title="Sword",Color="Gray", Price=2}
我知道,我应该使用 GROUPBY
或鲜明
,但了解如何获得所需要的。
I know that I should use GroupBy
or Distinct
, but understand how to get what is needed
List<Product> result = pr.GroupBy(g => g.Title, g.Price).ToList(); //not working
List<Product> result = pr.Distinct(...);
请帮忙
推荐答案
这是由群体需要的属性,然后选择:
It's groups by needed properties and select:
List<Product> result = pr.GroupBy(g => new { g.Title, g.Price })
.Select(g => g.First())
.ToList();
这篇关于LINQ不同或多个属性组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文