C# 将嵌套的键值对分组到字典

C# 将嵌套的键值对分组到字典,c#,generics,dictionary,key-value,C#,Generics,Dictionary,Key Value,我有以下代码: using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; public class Test { static void Main() { var list = new List<KeyValuePair<int, KeyValuePair<int, User>>

我有以下代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;


public class Test
{
    static void Main()
    {

        var list = new List<KeyValuePair<int, KeyValuePair<int, User>>>
                        {
                            new KeyValuePair<int, KeyValuePair<int, User>>(1,new KeyValuePair<int, User>(1,new User {FirstName = "Name1"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(1,new KeyValuePair<int, User>(1,new User {FirstName = "Name2"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(1,new KeyValuePair<int, User>(2,new User {FirstName = "Name3"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(1,new KeyValuePair<int, User>(2,new User {FirstName = "Name4"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(2,new KeyValuePair<int, User>(3,new User {FirstName = "Name5"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(2,new KeyValuePair<int, User>(3,new User {FirstName = "Name6"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(2,new KeyValuePair<int, User>(3,new User {FirstName = "Name6"})),
                            new KeyValuePair<int, KeyValuePair<int, User>>(3,new KeyValuePair<int, User>(4,new User {FirstName = "Name7"})),
                        };
    }
}
public class User
{
    public string FirstName { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
公开课考试
{
静态void Main()
{
变量列表=新列表
{
新的KeyValuePair(1,新的KeyValuePair(1,新用户{FirstName=“Name1”})),
新的KeyValuePair(1,新的KeyValuePair(1,新用户{FirstName=“Name2”})),
新的KeyValuePair(1,新的KeyValuePair(2,新用户{FirstName=“Name3”})),
新的KeyValuePair(1,新的KeyValuePair(2,新用户{FirstName=“Name4”})),
新的KeyValuePair(2,新的KeyValuePair(3,新用户{FirstName=“Name5”})),
新的KeyValuePair(2,新的KeyValuePair(3,新用户{FirstName=“Name6”})),
新的KeyValuePair(2,新的KeyValuePair(3,新用户{FirstName=“Name6”})),
新的KeyValuePair(3,新的KeyValuePair(4,新用户{FirstName=“Name7”}),
};
}
}
公共类用户
{
公共字符串名{get;set;}
}
如上所示,对于第一个键值对,同一个键有多个值,而且(在第二个嵌套键值对中)有多个相同的键。现在,我想将它们分组,并将列表对象转换为字典,其中键将是相同的(1,2,如上所示)但第一个值是dictionary,第二个值是collection,如下所示:

var outputNeeded = new Dictionary<int,Dictionary<int,Collection<User>>>();
var outputRequired=new Dictionary();
我怎么做

您可以使用LINQ:

var result = list
    .GroupBy(
        x => x.Key,
        x => x.Value)
    .ToDictionary(
        g => g.Key,
        g => g.GroupBy(
                  y => y.Key,
                  y => y.Value)
              .ToDictionary(
                  h => h.Key,
                  h => new Collection<User>(h.ToList())));

不清楚您想要输出什么。请更好地解释一下我的dtb,非常感谢您的回复!。我需要的最后一部分是集合,而不是列表。你能建议一下怎么做吗?@Rocky Singh:把清单整理成一个集合就行了。 1 \_ 1 | \_ Name1 | \_ Name2 \_ 2 \_ Name3 \_ Name4 2 \_ 3 \_ Name5 \_ Name6 \_ Name6 3 \_ 4 \_ Name7
var result = list
    .ToLookup(
        x => Tuple.Create(x.Key, x.Value.Key),
        x => x.Value.Value);