Linq query to get the distinct values in a list

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

    class Member {
        public string CategoryId {get; set;}
        public string MemberName{get; set;}
        public int Distance{get; set;}
    }
    
    var list = new List();
    list.Add(new Member{ CategoryId = "01", MemberName="andy", Distance=3});
    list.Add(new Member{ CategoryId = "02", MemberName="john", Distance=5});
    list.Add(new Member{ CategoryId = "01", MemberName="mathew", Distance=7});
    list.Add(new Member{ CategoryId = "03", MemberName="bakara", Distance=2});
    
    var query = list.GroupBy(member => member.CategoryId).Select(x=>x.OrderBy(y=>y.Distance).First());
    

提交回复
热议问题