[英]Using Linq to group a list of objects into a new grouped list of list of objects
I don't know if this is possible in Linq but here goes...我不知道这在 Linq 中是否可行,但这里...
I have an object:我有一个 object:
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public int GroupID { get; set; }
}
I return a list that may look like the following:我返回一个可能如下所示的列表:
List<User> userList = new List<User>();
userList.Add( new User { UserID = 1, UserName = "UserOne", GroupID = 1 } );
userList.Add( new User { UserID = 2, UserName = "UserTwo", GroupID = 1 } );
userList.Add( new User { UserID = 3, UserName = "UserThree", GroupID = 2 } );
userList.Add( new User { UserID = 4, UserName = "UserFour", GroupID = 1 } );
userList.Add( new User { UserID = 5, UserName = "UserFive", GroupID = 3 } );
userList.Add( new User { UserID = 6, UserName = "UserSix", GroupID = 3 } );
I want to be able to run a Linq query on the above list that groups all the users by GroupID.我希望能够在上面的列表上运行 Linq 查询,该查询按 GroupID 对所有用户进行分组。 So the output will be a list of user lists that contains user (if that makes sense?).
所以 output 将是包含用户的用户列表列表(如果这有意义?)。 Something like:
就像是:
GroupedUserList
UserList
UserID = 1, UserName = "UserOne", GroupID = 1
UserID = 2, UserName = "UserTwo", GroupID = 1
UserID = 4, UserName = "UserFour", GroupID = 1
UserList
UserID = 3, UserName = "UserThree", GroupID = 2
UserList
UserID = 5, UserName = "UserFive", GroupID = 3
UserID = 6, UserName = "UserSix", GroupID = 3
I've tried using the groupby linq clause but this seems to return a list of keys and its not grouped by correctly:我试过使用 groupby linq 子句,但这似乎返回了一个键列表并且它没有正确分组:
var groupedCustomerList = userList.GroupBy( u => u.GroupID ).ToList();
var groupedCustomerList = userList
.GroupBy(u => u.GroupID)
.Select(grp => grp.ToList())
.ToList();
Your group statement will group by group ID.您的组语句将按组 ID 分组。 For example, if you then write:
例如,如果你然后写:
foreach (var group in groupedCustomerList)
{
Console.WriteLine("Group {0}", group.Key);
foreach (var user in group)
{
Console.WriteLine(" {0}", user.UserName);
}
}
that should work fine.那应该可以正常工作。 Each group has a key, but also contains an
IGrouping<TKey, TElement>
which is a collection that allows you to iterate over the members of the group.每个组都有一个键,但也包含一个
IGrouping<TKey, TElement>
,它是一个允许您迭代组成员的集合。 As Lee mentions, you can convert each group to a list if you really want to, but if you're just going to iterate over them as per the code above, there's no real benefit in doing so.正如 Lee 提到的,如果您真的愿意,您可以将每个组转换为一个列表,但是如果您只是按照上面的代码迭代它们,那么这样做并没有真正的好处。
For type对于类型
public class KeyValue
{
public string KeyCol { get; set; }
public string ValueCol { get; set; }
}
collection收藏
var wordList = new Model.DTO.KeyValue[] {
new Model.DTO.KeyValue {KeyCol="key1", ValueCol="value1" },
new Model.DTO.KeyValue {KeyCol="key2", ValueCol="value1" },
new Model.DTO.KeyValue {KeyCol="key3", ValueCol="value2" },
new Model.DTO.KeyValue {KeyCol="key4", ValueCol="value2" },
new Model.DTO.KeyValue {KeyCol="key5", ValueCol="value3" },
new Model.DTO.KeyValue {KeyCol="key6", ValueCol="value4" }
};
our linq query look like below我们的 linq 查询如下所示
var query =from m in wordList group m.KeyCol by m.ValueCol into g
select new { Name = g.Key, KeyCols = g.ToList() };
or for array instead of list like below或用于数组而不是像下面这样的列表
var query =from m in wordList group m.KeyCol by m.ValueCol into g
select new { Name = g.Key, KeyCols = g.ToList().ToArray<string>() };
Still an old one, but answer from Lee did not give me the group.Key as result.仍然是旧的,但李的回答没有给我组。结果是关键。 Therefore, I am using the following statement to group a list and return a grouped list:
因此,我使用以下语句对列表进行分组并返回分组列表:
public IOrderedEnumerable<IGrouping<string, User>> groupedCustomerList;
groupedCustomerList =
from User in userList
group User by User.GroupID into newGroup
orderby newGroup.Key
select newGroup;
Each group now has a key, but also contains an IGrouping which is a collection that allows you to iterate over the members of the group.每个组现在都有一个键,但还包含一个 IGrouping,它是一个允许您迭代组成员的集合。
Nowadays you can use Linq's FindAll()现在你可以使用 Linq 的 FindAll()
var groupedCustomerList = userList.FindAll( u => u.GroupID);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.