假设我有三个用户,我想按照他们的国家对他们进行分组。我会这样做:
var users = new[]
{
new User { Name = "Phil", Country = "UK" },
new User { Name = "John", Country = "UK" },
new User { Name = "Mike", Country = "USA" }
};
List<IGrouping<string, User>> groupedUsers
= users.GroupBy(user => user.Country).ToList();
现在假设我的程序后来又获得了三个用户,所以我也将他们分组:
var moreUsers = new[]
{
new User { Name = "Phoebe", Country = "AUS" },
new User { Name = "Joan", Country = "UK" },
new User { Name = "Mindy", Country = "USA" }
};
List<IGrouping<string, User>> moreGroupedUsers
= moreUsers.GroupBy(user => user.Country).ToList();
我现在有两个独立的分组,groupedUsers
和 moreGroupedUsers
。如何将它们合并成一个,同时保持分组有效?