views:

41

answers:

1

Hi,

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. ??

+2  A: 

You can use 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())));

This creates the following hierarchy:

1
 \_ 1
 |   \_ Name1
 |   \_ Name2
 \_ 2
     \_ Name3
     \_ Name4
2
 \_ 3
     \_ Name5
     \_ Name6
     \_ Name6
3
 \_ 4
     \_ Name7

Nested dictionaries are often not very nice to use, though. I'd probably prefer a simple lookup table:

var result = list
    .ToLookup(
        x => Tuple.Create(x.Key, x.Value.Key),
        x => x.Value.Value);
dtb
Hi dtb, thanks for your response very close!. The last portion I need is the collection instead of List . Can you please suggest how to do it ?
Rocky Singh
@Rocky Singh: Just wrap the list in a Collection<User>.
dtb
Thanks for your support :)
Rocky Singh