Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ - convert nested dictionary to a list

Tags:

c#

linq

How do I flatten a nested dictionary into a list of some objects (SomeObject in the following example) which should hold keys of those dictionaries?

For example: let's have a dictionary of the following type

var nestedDictionary = new Dictionary<int, Dictionary<int, string>>();

then, let's have this class

public class SomeObject
{
   public int var1;
   public int var2;
   public string someStringVar;
}

How do I convert nestedDictionary to a List<SomeObject> where var1 is the key of the outer dictionary, var2 is the key of the inner dictionary and someStringVar is the string value of the inner dictionary?

Essentially, how do I transfer this:

nestedDict[0][0] = "foo";
nestedDict[0][1] = "bar";
nestedDict[0][2] = "foo1";
nestedDict[1][0] = "bar1";
nestedDict[1][1] = "foo2";
nestedDict[1][2] = "bar2";

to this (in pseudo C# just to visualize it)

objList[0] = SomeObject { var1 = 0, var2 = 0, someStringVar = "foo" }
objList[1] = SomeObject { var1 = 0, var2 = 1, someStringVar = "bar" }
objList[2] = SomeObject { var1 = 0, var2 = 2, someStringVar = "foo1" }
objList[3] = SomeObject { var1 = 1, var2 = 0, someStringVar = "bar1" }
objList[4] = SomeObject { var1 = 1, var2 = 1, someStringVar = "foo2" }
objList[5] = SomeObject { var1 = 1, var2 = 2, someStringVar = "bar2" }

using LINQ?

like image 884
Mirek Avatar asked Jul 13 '12 09:07

Mirek


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

You can use SelectMany() and write something like:

var objList = nestedDictionary.SelectMany(
    pair => pair.Value.Select(
        innerPair => new SomeObject() {
            var1 = pair.Key,
            var2 = innerPair.Key,
            someStringVar = innerPair.Value
        })).ToList();
like image 139
Frédéric Hamidi Avatar answered Oct 22 '22 21:10

Frédéric Hamidi


This should work:

var flattened =
from kvpOuter in nestedDictionary
from kvpInner in kvpOuter.Value
select new SomeObject()
{
    var1 = kvpOuter.Key,
    var2 = kvpInner.Key,
    someStringVar = kvpInner.Value
};
var list = flattened.ToList(); // if you need a list...
like image 44
digEmAll Avatar answered Oct 22 '22 23:10

digEmAll