Linq query to get the distinct values in a list

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

    try this

    var distinctIds = list.Distinct(item => item.CategoryId).ToList();
    var newList = new List();
    foreach(var id in distinctIds){
          newList.Add(list.Where(item => item.CategoryId == id).Min(item => item.Distance))
    }
    newList.OrderBy(item => item.CategoryId);
    

提交回复
热议问题