LINQ's Distinct() on a particular property

后端 未结 20 2035
一向
一向 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:51

    You can do it (albeit not lightning-quickly) like so:

    people.Where(p => !people.Any(q => (p != q && p.Id == q.Id)));
    

    That is, "select all people where there isn't another different person in the list with the same ID."

    Mind you, in your example, that would just select person 3. I'm not sure how to tell which you want, out of the previous two.

提交回复
热议问题