Remove objects with a duplicate property from List

后端 未结 4 560
陌清茗
陌清茗 2021-02-04 23:03

I have a List of objects in C#. All of the objects contain a property ID. There are several objects that have the same ID property.

How can I trim the List (or make a

4条回答
  •  滥情空心
    2021-02-04 23:46

    If you want to avoid using a third-party library, you could do something like:

    var bar = fooArray.GroupBy(x => x.Id).Select(x => x.First()).ToList();
    

    That will group the array by the Id property, then select the first entry in the grouping.

提交回复
热议问题