linq query to return distinct field values from a list of objects

前端 未结 7 1829
醉梦人生
醉梦人生 2020-12-24 11:42
class obj
{
    int typeId; //10 types  0-9 
    string uniqueString; //this is unique
}

Assume there is list with 100 elements of obj, but only 10

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 12:02

    Sure, use Enumerable.Distinct.

    Given a collection of obj (e.g. foo), you'd do something like this:

    var distinctTypeIDs = foo.Select(x => x.typeID).Distinct();
    

提交回复
热议问题