我有以下代码:分组嵌套KEYVALUE对到字典
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>>>();
我该怎么做。 ??
它不清楚你想输出。请解释一下好吧 – Neowizard 2010-10-20 14:04:10