Linq query to get the distinct values in a list

后端 未结 7 1865
攒了一身酷
攒了一身酷 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:19

    This also works, if you do not require items ordered by (little modified Kjartan's answer)

    var grouped = list.GroupBy(item => item.CategoryId).ToList();
    var shortest = grouped.Select(grp => grp.OrderBy(item => item.Distance).First());
    

提交回复
热议问题