How can I use Linq to get distinct results filtered by property
This question already has an answer here:
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
上一篇: 仅在内部列出不同的值