Linq query to get the distinct values in a list

后端 未结 7 1740
攒了一身酷
攒了一身酷 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<Member>();
    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());
    
    0 讨论(0)
提交回复
热议问题