我如何使用Linq获取按属性过滤的独特结果

这个问题在这里已经有了答案:

  • LINQ对特定属性的Distinct()17的答案

  • 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

    链接地址: http://www.djcxy.com/p/51305.html

    上一篇: How can I use Linq to get distinct results filtered by property

    下一篇: Remove Duplicate Titles from list in MVC