我如何使用Linq获取按属性过滤的独特结果
这个问题在这里已经有了答案:
List<Tool> distinctTool = tools
.GroupBy(p => p.Number)
.Select(g => g.OrderByDescending(q => q.Source == "M1").First())
.ToList();
您需要按照编号对Tools
进行分组,并获取其中的First()
List<Tool> distinctTool = tools
.GroupBy(p => p.Number)
.Select(g => g.OrderByDescending(q => q.Source == "M1").First())
.ToList();
尝试按数字分组并获取第一个元素,
List<Tool> distinctTool = tools.GroupBy(p => p.Number)
.Select(f => f.OrderByDescending(q => q.Source == "M1").First())
.ToList();
WORKING FIDDLE
上一篇: How can I use Linq to get distinct results filtered by property