使用LINQ对对象列表进行分组

时间:2013-09-02 02:42:20

标签: c# linq group-by linq-to-objects

我有一个对象:

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

我返回的列表可能如下所示:

List<Customer> CustomerList = new List<Customer>();
CustomerList.Add( new Customer { ID = 1, Name = "One", GroupID = 1 } );
CustomerList.Add( new Customer { ID = 2, Name = "Two", GroupID = 1 } );
CustomerList.Add( new Customer { ID = 3, Name = "Three", GroupID = 2 } );
CustomerList.Add( new Customer { ID = 4, Name = "Four", GroupID = 1 } );
CustomerList.Add( new Customer { ID = 5, Name = "Five", GroupID = 3 } );
CustomerList.Add( new Customer { ID = 6, Name = "Six", GroupID = 3 } );

我想返回一个看起来像

的linq查询
CustomerList
  GroupID =1
    UserID = 1, UserName = "UserOne", GroupID = 1
    UserID = 2, UserName = "UserTwo", GroupID = 1
    UserID = 4, UserName = "UserFour", GroupID = 1
  GroupID =2
    UserID = 3, UserName = "UserThree", GroupID = 2
  GroupID =3
    UserID = 5, UserName = "UserFive", GroupID = 3
    UserID = 6, UserName = "UserSix",

我试过

Using Linq to group a list of objects into a new grouped list of list of objects

var groupedCustomerList = CustomerList
  .GroupBy(u => u.GroupID)
  .Select(grp => grp.ToList())
  .ToList();

有效但无法提供所需的输出。

5 个答案:

答案 0 :(得分:69)

var groupedCustomerList = CustomerList.GroupBy(u => u.GroupID)
                                      .Select(grp =>new { GroupID =grp.Key, CustomerList = grp.ToList()})
                                      .ToList();

答案 1 :(得分:4)

var groupedCustomerList = CustomerList
                         .GroupBy(u => u.GroupID, u=>{
                                                        u.Name = "User" + u.Name;
                                                        return u;
                                                     }, (key,g)=>g.ToList())
                         .ToList();

如果您不想更改原始数据,您应该在类中添加一些方法(克隆和修改的方式),如下所示:

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();

我认为您可能希望以不同方式显示 Customer而不修改原始数据。如果是这样,您应该以不同方式设计您的课程Customer,如下所示:

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();

答案 2 :(得分:1)

这就是你想要的吗?

var grouped = CustomerList.GroupBy(m => m.GroupID).Select((n) => new { GroupId = n.Key, Items = n.ToList() });

答案 3 :(得分:0)

可以使用IGrouping获得所需的结果,{{3}}表示具有公共密钥的对象集合,在这种情况下为GroupID

 var newCustomerList = CustomerList.GroupBy(u => u.GroupID)
                                                  .Select(group => new { GroupID = group.Key, Customers = group.ToList() })
                                                  .ToList();

答案 4 :(得分:0)

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 })); 
  }
}