使用Linq将一个对象列表分组到一个新的对象列表分组列表中

lnvxswe2  于 2022-12-06  发布在  其他
关注(0)|答案(5)|浏览(163)

我不知道这在Linq是否可能,但现在开始...
我有一个对象:

public class User
{
  public int UserID { get; set; }
  public string UserName { get; set; }
  public int GroupID { get; set; }
}

我返回一个列表,可能如下所示:

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

我希望能够在上面的列表上运行一个Linq查询,该列表按GroupID对所有用户进行分组。因此,输出将是一个包含user的用户列表列表(如果这有意义的话?)。类似于:

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

我试过使用groupby linq子句,但这似乎返回了一个键列表,并且它的分组方式不正确:

var groupedCustomerList = userList.GroupBy( u => u.GroupID ).ToList();
0s7z1bwu

0s7z1bwu1#

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

kmpatx3s2#

您的group语句 * 将 * 按组ID分组。例如,如果您编写:

foreach (var group in groupedCustomerList)
{
    Console.WriteLine("Group {0}", group.Key);
    foreach (var user in group)
    {
        Console.WriteLine("  {0}", user.UserName);
    }
}

这应该可以很好地工作。每个组 * 有 * 一个键,但也包含一个IGrouping<TKey, TElement>,它是一个集合,允许你迭代组中的成员。正如Lee提到的,如果你真的想,你可以将每个组转换为列表,但如果你只是按照上面的代码迭代它们,这样做没有真正的好处。

zf9nrax1

zf9nrax13#

对于类型

public class KeyValue
{
    public string KeyCol { get; set; }
    public string ValueCol { get; set; }
}

收藏

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

我们linq查询如下所示

var query =from m in wordList group m.KeyCol by m.ValueCol into g
select new { Name = g.Key, KeyCols = g.ToList() };

或用于数组而不是列表,如下所示

var query =from m in wordList group m.KeyCol by m.ValueCol into g
select new { Name = g.Key, KeyCols = g.ToList().ToArray<string>() };
bxgwgixi

bxgwgixi4#

仍然是一个旧的,但是Lee的回答没有给予我group.key作为结果。因此,我使用下面的语句来分组列表并返回分组后的列表:

public IOrderedEnumerable<IGrouping<string, User>> groupedCustomerList;

groupedCustomerList =
        from User in userList
        group User by User.GroupID into newGroup
        orderby newGroup.Key
        select newGroup;

现在,每个组都有一个键,但还包含一个IGrouping,它是一个集合,允许您循环访问组的成员。

agxfikkp

agxfikkp5#

Nowadays you can use Linq's FindAll()

var groupedCustomerList = userList.FindAll( u => u.GroupID);

相关问题