将嵌套键值对分组到字典中
我有以下代码:
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; }
}
上面您可以看到第一个键值对的相同键有多个值,并且进一步(在第二个嵌套键值对中)有多个相同的键现在我想对它们进行分组并将列表对象转换为字典其中键将相同(如上所示的 1,2),但第一个值将是字典,第二个值将是集合。像这样:
var outputNeeded = new Dictionary<int,Dictionary<int,Collection<User>>>();
我该怎么做。 ??
I have the following code:
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; }
}
Above as you can see there are multiple values for same key for first KeyValue Pair and further there(in second nested key Value Pair) are multiple same keys Now I want to group them and convert the list object to dictionary in which the Key will be the same(1,2 as shown above) but the first value will be dictionary and value for second will be the collection.Like this:
var outputNeeded = new Dictionary<int,Dictionary<int,Collection<User>>>();
How can I do it. ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 LINQ:
这会创建以下层次结构:
不过,嵌套字典通常不太好用。
我可能更喜欢一个简单的查找表:
You can use LINQ:
This creates the following hierarchy:
Nested dictionaries are often not very nice to use, though.
I'd probably prefer a simple lookup table: