のリスト:私は次のように見えるかもしれリストを返す私はオブジェクト持つオブジェクト
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();
作品が、所望の出力を与えるものではありません。
[の可能性のある重複オブジェクトの一覧の編集リスト](http://stackoverflow.com/questions/2697253/using-linq-to-group-a-list-of-objects-into-a-new-grouped-list-of-list-オブジェクトの) – scar80