Using LINQ to group a list of objects

前端 未结 5 1898
攒了一身酷
攒了一身酷 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:12
    var groupedCustomerList = CustomerList
                             .GroupBy(u => u.GroupID, u=>{
                                                            u.Name = "User" + u.Name;
                                                            return u;
                                                         }, (key,g)=>g.ToList())
                             .ToList();
    

    If you don't want to change the original data, you should add some method (kind of clone and modify) to your class like this:

    public class Customer {
      public int ID { get; set; }
      public string Name { get; set; }
      public int GroupID { get; set; }
      public Customer CloneWithNamePrepend(string prepend){
        return new Customer(){
              ID = this.ID,
              Name = prepend + this.Name,
              GroupID = this.GroupID
         };
      }
    }    
    //Then
    var groupedCustomerList = CustomerList
                             .GroupBy(u => u.GroupID, u=>u.CloneWithNamePrepend("User"), (key,g)=>g.ToList())
                             .ToList();
    

    I think you may want to display the Customer differently without modifying the original data. If so you should design your class Customer differently, like this:

    public class Customer {
      public int ID { get; set; }
      public string Name { get; set; }
      public int GroupID { get; set; }
      public string Prefix {get;set;}
      public string FullName {
        get { return Prefix + Name;}
      }            
    }
    //then to display the fullname, just get the customer.FullName; 
    //You can also try adding some override of ToString() to your class
    
    var groupedCustomerList = CustomerList
                             .GroupBy(u => {u.Prefix="User", return u.GroupID;} , (key,g)=>g.ToList())
                             .ToList();
    
    0 讨论(0)
  • is this what you want?

    var grouped = CustomerList.GroupBy(m => m.GroupID).Select((n) => new { GroupId = n.Key, Items = n.ToList() });
    
    0 讨论(0)
  • 2020-12-08 15:19

    The desired result can be obtained using IGrouping, which represents a collection of objects that have a common key in this case a GroupID

     var newCustomerList = CustomerList.GroupBy(u => u.GroupID)
                                                      .Select(group => new { GroupID = group.Key, Customers = group.ToList() })
                                                      .ToList();
    
    0 讨论(0)
  • 2020-12-08 15:22
    var groupedCustomerList = CustomerList.GroupBy(u => u.GroupID)
                                          .Select(grp =>new { GroupID =grp.Key, CustomerList = grp.ToList()})
                                          .ToList();
    
    0 讨论(0)
  • 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 })); 
      }
    }
    
    0 讨论(0)
提交回复
热议问题