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

前端 未结 7 1822
醉梦人生
醉梦人生 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 11:52

    I think this is what your looking for:

        var objs= (from c in List_Objects 
    orderby c.TypeID  select c).GroupBy(g=>g.TypeID).Select(x=>x.FirstOrDefault());      
    

    Similar to this Returning a Distinct IQueryable with LINQ?

提交回复
热议问题