1
votes

I have a table with a structure like this:

Column1 AS INT,

Column2 AS INT,

Column3 AS INT,

Column4 AS DECIMAL

My LINQ query is this:

var query = from t in context.Table select t;

I want to convert/transform the query to this Dictionary object:

Dictionary<int, Dictionary<int, Dictionary<int, decimal>>>

using only the ToDictionary method of the LINQ query. Can it be done?

1
@acermate433s, I don't understand how Column1...Column4 relate to your Dictionary<Employee, Dictionary<int, Dictionary<int, decimal>>>. Please explain what column you want to match with what part of the dictionary. And where does Employee come in? - Kirk Woll
Reading "Dictionary<int, Dictionary<int, Dictionary<int, decimal>>>" is a brainfsk by itself. Are you sure you want to do this? - Judah Gabriel Himango
A dictionary within a dictionary within a dictionary... Inception! - Ahmad Mageed
@Kirk: I've edited my question, it should have been an int. - acermate433s
Basically I want to create a look-up table. I would want to access the items using this notation Dictionary[][][].Value - acermate433s

1 Answers

1
votes

I believe this will give you what you want:

var result = data
    .GroupBy(x => x.Column1)
    .ToDictionary(x => x.Key, x => x
        .GroupBy(y => y.Column2)
        .ToDictionary(y => y.Key, y => y
            .ToDictionary(z => z.Column3, z => z.Column4)));

Though I agree with Judah that only ugly code will result from using it.