Using LINQ to group a list of objects

前端 未结 5 1920
攒了一身酷
攒了一身酷 2020-12-08 14:34

I have an object:

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int GroupID { get; set; }
}
         


        
5条回答
  •  忘掉有多难
    2020-12-08 15:22

    var result = from cx in CustomerList
             group cx by cx.GroupID into cxGroup
         orderby cxGroup.Key
         select cxGroup; 
    
    foreach (var cxGroup in result) { 
    Console.WriteLine(String.Format("GroupID = {0}", cxGroup.Key)); 
      foreach (var cx in cxGroup) { 
        Console.WriteLine(String.Format("\tUserID = {0}, UserName = {1}, GroupID = {2}", 
          new object[] { cx.ID, cx.Name, cx.GroupID })); 
      }
    }
    

提交回复
热议问题