Linq query to get the distinct values in a list

后端 未结 7 1738
攒了一身酷
攒了一身酷 2021-02-08 22:34

Suppose this is my member class

class Member 
{
    public string CategoryId { get; set; }
    public string MemberName { get; set; }
    public int Distance { g         


        
7条回答
  •  长情又很酷
    2021-02-08 23:02

    This is similar to those above, however it is technically a one liner for those interested....

    var queryResult= (from l in list
                      select new Member()
                     { 
                        CategoryId = l.CategoryId , 
                        MemberName = l.MemberName 
                        Distance = l.Distance
                     }).GroupBy(x=>x.CatagoryId).Select(z=>z.OrderBy(i=>i.Distance).First()).ToList();
    

提交回复
热议问题