LINQ's Distinct() on a particular property

后端 未结 20 2014
一向
一向 2020-11-21 05:05

I am playing with LINQ to learn about it, but I can\'t figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy

20条回答
  •  滥情空心
    2020-11-21 05:40

    I've written an article that explains how to extend the Distinct function so that you can do as follows:

    var people = new List();
    
    people.Add(new Person(1, "a", "b"));
    people.Add(new Person(2, "c", "d"));
    people.Add(new Person(1, "a", "b"));
    
    foreach (var person in people.Distinct(p => p.ID))
        // Do stuff with unique list here.
    

    Here's the article (now in the Web Archive): Extending LINQ - Specifying a Property in the Distinct Function

提交回复
热议问题