C# remove entries from list based on a distinct field

This question already has an answer here:

  • LINQ's Distinct() on a particular property 17 answers

  • Make propertyBs a HashSet<string> instead of a List<string> , and use the return value of Add to make a decision:

    var propertyBs = new HashSet<string>();
    var res = models.Where(m => propertyBs.Add(m.PropertyB)).ToList();
    

    When you call Add on hash set, only the addition of the initial item returns true ; all duplicate additions would return false , so their corresponding models would be filtered out.


    I possible solution is using the Linq Group by feature. Take a look into this: Group by in LINQ

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

    上一篇: 从列表中删除重复的值

    下一篇: C#根据不同的字段从列表中删除条目