LINQ's Distinct() on a particular property

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

    Use:

    List pList = new List();
    /* Fill list */
    
    var result = pList.Where(p => p.Name != null).GroupBy(p => p.Id).Select(grp => grp.FirstOrDefault());
    

    The where helps you filter the entries (could be more complex) and the groupby and select perform the distinct function.

提交回复
热议问题