C# Generate Nested Dictionaries from the a List












3














I managed to pull the following data from the database into a List<> using Entity Framework.



id      Variable        Value   Coef
--------------------------------------
1000 Gender Male 0
1001 Gender Female -0.205
1009 College Code AT -1.732
1010 College Code BU -1.806
1011 College Code EH -1.728
1012 College Code EN -2.003
1013 College Code LF -1.779
1014 College Code pp -2.042
1015 College Code SC -2.070
1016 College Code UC -1.845
1017 AGI AGI N/A 0.236
1018 AGI 0 -0.684


I am a bit new to C#, so I wanted to know what would be the best way to create a nested Dictionary with the following format:



//to construct a dictionary to hold Dictionary<Variable, {Value, Coef}>
Dictionary<string, Dictionary<string, double>> data = Dictionary<string, Dictionary<string, double>>();


So, for instance, I could access the data like this:



Console.WriteLine(data['Gender']['Male']) //returns 0
Console.WriteLine(data['College Code']['LF']) //returns -1.779









share|improve this question




















  • 1




    What have you tried so far?
    – John Wu
    Nov 19 '18 at 18:47






  • 2




    It would be simpler if you combined those values into one key like data["College Code=LF"]. If you can't do that then you need a custom type for the first lookup with its own string indexer that returns a IDictionary<string,decimal> implementation.
    – Igor
    Nov 19 '18 at 18:48












  • @JohnWu I have tried to do it with nested foreach loop but I ended up with nonsensical result. Basically I got stuck.
    – jax
    Nov 19 '18 at 18:50






  • 1




    @Igor That is a good idea. I will try that but will check back.
    – jax
    Nov 19 '18 at 18:52










  • Shouldn't data["Gender"]["Male"] return 0?
    – juharr
    Nov 19 '18 at 19:10
















3














I managed to pull the following data from the database into a List<> using Entity Framework.



id      Variable        Value   Coef
--------------------------------------
1000 Gender Male 0
1001 Gender Female -0.205
1009 College Code AT -1.732
1010 College Code BU -1.806
1011 College Code EH -1.728
1012 College Code EN -2.003
1013 College Code LF -1.779
1014 College Code pp -2.042
1015 College Code SC -2.070
1016 College Code UC -1.845
1017 AGI AGI N/A 0.236
1018 AGI 0 -0.684


I am a bit new to C#, so I wanted to know what would be the best way to create a nested Dictionary with the following format:



//to construct a dictionary to hold Dictionary<Variable, {Value, Coef}>
Dictionary<string, Dictionary<string, double>> data = Dictionary<string, Dictionary<string, double>>();


So, for instance, I could access the data like this:



Console.WriteLine(data['Gender']['Male']) //returns 0
Console.WriteLine(data['College Code']['LF']) //returns -1.779









share|improve this question




















  • 1




    What have you tried so far?
    – John Wu
    Nov 19 '18 at 18:47






  • 2




    It would be simpler if you combined those values into one key like data["College Code=LF"]. If you can't do that then you need a custom type for the first lookup with its own string indexer that returns a IDictionary<string,decimal> implementation.
    – Igor
    Nov 19 '18 at 18:48












  • @JohnWu I have tried to do it with nested foreach loop but I ended up with nonsensical result. Basically I got stuck.
    – jax
    Nov 19 '18 at 18:50






  • 1




    @Igor That is a good idea. I will try that but will check back.
    – jax
    Nov 19 '18 at 18:52










  • Shouldn't data["Gender"]["Male"] return 0?
    – juharr
    Nov 19 '18 at 19:10














3












3








3







I managed to pull the following data from the database into a List<> using Entity Framework.



id      Variable        Value   Coef
--------------------------------------
1000 Gender Male 0
1001 Gender Female -0.205
1009 College Code AT -1.732
1010 College Code BU -1.806
1011 College Code EH -1.728
1012 College Code EN -2.003
1013 College Code LF -1.779
1014 College Code pp -2.042
1015 College Code SC -2.070
1016 College Code UC -1.845
1017 AGI AGI N/A 0.236
1018 AGI 0 -0.684


I am a bit new to C#, so I wanted to know what would be the best way to create a nested Dictionary with the following format:



//to construct a dictionary to hold Dictionary<Variable, {Value, Coef}>
Dictionary<string, Dictionary<string, double>> data = Dictionary<string, Dictionary<string, double>>();


So, for instance, I could access the data like this:



Console.WriteLine(data['Gender']['Male']) //returns 0
Console.WriteLine(data['College Code']['LF']) //returns -1.779









share|improve this question















I managed to pull the following data from the database into a List<> using Entity Framework.



id      Variable        Value   Coef
--------------------------------------
1000 Gender Male 0
1001 Gender Female -0.205
1009 College Code AT -1.732
1010 College Code BU -1.806
1011 College Code EH -1.728
1012 College Code EN -2.003
1013 College Code LF -1.779
1014 College Code pp -2.042
1015 College Code SC -2.070
1016 College Code UC -1.845
1017 AGI AGI N/A 0.236
1018 AGI 0 -0.684


I am a bit new to C#, so I wanted to know what would be the best way to create a nested Dictionary with the following format:



//to construct a dictionary to hold Dictionary<Variable, {Value, Coef}>
Dictionary<string, Dictionary<string, double>> data = Dictionary<string, Dictionary<string, double>>();


So, for instance, I could access the data like this:



Console.WriteLine(data['Gender']['Male']) //returns 0
Console.WriteLine(data['College Code']['LF']) //returns -1.779






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 1:10









Tetsuya Yamamoto

14.7k42040




14.7k42040










asked Nov 19 '18 at 18:45









jaxjax

4251128




4251128








  • 1




    What have you tried so far?
    – John Wu
    Nov 19 '18 at 18:47






  • 2




    It would be simpler if you combined those values into one key like data["College Code=LF"]. If you can't do that then you need a custom type for the first lookup with its own string indexer that returns a IDictionary<string,decimal> implementation.
    – Igor
    Nov 19 '18 at 18:48












  • @JohnWu I have tried to do it with nested foreach loop but I ended up with nonsensical result. Basically I got stuck.
    – jax
    Nov 19 '18 at 18:50






  • 1




    @Igor That is a good idea. I will try that but will check back.
    – jax
    Nov 19 '18 at 18:52










  • Shouldn't data["Gender"]["Male"] return 0?
    – juharr
    Nov 19 '18 at 19:10














  • 1




    What have you tried so far?
    – John Wu
    Nov 19 '18 at 18:47






  • 2




    It would be simpler if you combined those values into one key like data["College Code=LF"]. If you can't do that then you need a custom type for the first lookup with its own string indexer that returns a IDictionary<string,decimal> implementation.
    – Igor
    Nov 19 '18 at 18:48












  • @JohnWu I have tried to do it with nested foreach loop but I ended up with nonsensical result. Basically I got stuck.
    – jax
    Nov 19 '18 at 18:50






  • 1




    @Igor That is a good idea. I will try that but will check back.
    – jax
    Nov 19 '18 at 18:52










  • Shouldn't data["Gender"]["Male"] return 0?
    – juharr
    Nov 19 '18 at 19:10








1




1




What have you tried so far?
– John Wu
Nov 19 '18 at 18:47




What have you tried so far?
– John Wu
Nov 19 '18 at 18:47




2




2




It would be simpler if you combined those values into one key like data["College Code=LF"]. If you can't do that then you need a custom type for the first lookup with its own string indexer that returns a IDictionary<string,decimal> implementation.
– Igor
Nov 19 '18 at 18:48






It would be simpler if you combined those values into one key like data["College Code=LF"]. If you can't do that then you need a custom type for the first lookup with its own string indexer that returns a IDictionary<string,decimal> implementation.
– Igor
Nov 19 '18 at 18:48














@JohnWu I have tried to do it with nested foreach loop but I ended up with nonsensical result. Basically I got stuck.
– jax
Nov 19 '18 at 18:50




@JohnWu I have tried to do it with nested foreach loop but I ended up with nonsensical result. Basically I got stuck.
– jax
Nov 19 '18 at 18:50




1




1




@Igor That is a good idea. I will try that but will check back.
– jax
Nov 19 '18 at 18:52




@Igor That is a good idea. I will try that but will check back.
– jax
Nov 19 '18 at 18:52












Shouldn't data["Gender"]["Male"] return 0?
– juharr
Nov 19 '18 at 19:10




Shouldn't data["Gender"]["Male"] return 0?
– juharr
Nov 19 '18 at 19:10












3 Answers
3






active

oldest

votes


















2














var data = _dbContext.Tbl.ToDictionary(_ => _.Variable + "=" + _.Value, _ => _.Coef, StringComparer.OrdinalIgnoreCase);


With the data you have shown there is no need to group, just create your composite key and specify the value. I also recommend using a case insensitive key.



You would then access the data this way where College Code=LF is the key.



Console.WriteLine(data["College Code=LF"]);





share|improve this answer





















  • great solution. I used foreach to do the same but this one is more elegant.
    – jax
    Nov 19 '18 at 19:30



















2














Using some initial data as follows



public class data
{
public int id { get; set; }
public string Variable { get; set; }
public string Value { get; set; }
public decimal Coef { get; set; }
}

var listy = new List<data>() {
new data() { id=1000, Variable="Gender", Value="Male", Coef=0m },
new data() { id=1001, Variable="Gender", Value="Female", Coef=-0.205m },
new data() { id=1009, Variable="College Code", Value="AT", Coef=-1.732m },
new data() { id=1010, Variable="College Code", Value="BU", Coef=-1.806m },
new data() { id=1011, Variable="College Code", Value="EH", Coef=-1.728m },
new data() { id=1012, Variable="College Code", Value="EN", Coef=-2.003m },
new data() { id=1013, Variable="College Code", Value="LF", Coef=-1.779m },
new data() { id=1014, Variable="College Code", Value="pp", Coef=-2.042m },
new data() { id=1015, Variable="College Code", Value="SC", Coef=-2.070m },
new data() { id=1016, Variable="College Code", Value="UC", Coef=-1.845m },
new data() { id=1017, Variable="AGI", Value="AGI N/A", Coef=0.236m },
new data() { id=1018, Variable="AGI", Value="0", Coef=-0.684m },
};


Get the distinct list of Variable fields to seed the outer dictionary, then find related items in the data source, and create an inner dictionary for those:



var b = listy
.Select(x => x.Variable)
.Distinct()
// outer dictionary, key is Variable
.ToDictionary(k => k, v =>
listy
// find items in the list with the same Variable
.Where(x => x.Variable == v)
// and create a dictionary for the Value/Coef pairs.
.ToDictionary(k2 => k2.Value, v2 => v2.Coef));


Some interactive shell output:



> b["AGI"]
Dictionary<string, decimal>(2) { { "AGI N/A", 0.236 }, { "0", -0.684 } }
> b["AGI"]["0"]
-0.684
> b["College Code"]["AT"]
-1.732
> b["College Code"]["BU"]
-1.806





share|improve this answer





















  • That is a good solution as well. Thank you.
    – jax
    Nov 19 '18 at 19:34










  • This is going to iterate the list twice which is completely unnecessary
    – juharr
    Nov 19 '18 at 20:43



















1














Given a collection of the date you should be able to get the results you want like this.



var lookup = data.GroupBy(x => x.Variable)
.ToDictionary(g => g.Key, g.ToDictionary(y => y.Value, y => y.Coef));


Note this will fail if you have multiple items with the same values in the Variable and Value columns.






share|improve this answer





















  • I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
    – jax
    Nov 19 '18 at 19:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380866%2fc-sharp-generate-nested-dictionaries-from-the-a-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














var data = _dbContext.Tbl.ToDictionary(_ => _.Variable + "=" + _.Value, _ => _.Coef, StringComparer.OrdinalIgnoreCase);


With the data you have shown there is no need to group, just create your composite key and specify the value. I also recommend using a case insensitive key.



You would then access the data this way where College Code=LF is the key.



Console.WriteLine(data["College Code=LF"]);





share|improve this answer





















  • great solution. I used foreach to do the same but this one is more elegant.
    – jax
    Nov 19 '18 at 19:30
















2














var data = _dbContext.Tbl.ToDictionary(_ => _.Variable + "=" + _.Value, _ => _.Coef, StringComparer.OrdinalIgnoreCase);


With the data you have shown there is no need to group, just create your composite key and specify the value. I also recommend using a case insensitive key.



You would then access the data this way where College Code=LF is the key.



Console.WriteLine(data["College Code=LF"]);





share|improve this answer





















  • great solution. I used foreach to do the same but this one is more elegant.
    – jax
    Nov 19 '18 at 19:30














2












2








2






var data = _dbContext.Tbl.ToDictionary(_ => _.Variable + "=" + _.Value, _ => _.Coef, StringComparer.OrdinalIgnoreCase);


With the data you have shown there is no need to group, just create your composite key and specify the value. I also recommend using a case insensitive key.



You would then access the data this way where College Code=LF is the key.



Console.WriteLine(data["College Code=LF"]);





share|improve this answer












var data = _dbContext.Tbl.ToDictionary(_ => _.Variable + "=" + _.Value, _ => _.Coef, StringComparer.OrdinalIgnoreCase);


With the data you have shown there is no need to group, just create your composite key and specify the value. I also recommend using a case insensitive key.



You would then access the data this way where College Code=LF is the key.



Console.WriteLine(data["College Code=LF"]);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 19:25









IgorIgor

39k347100




39k347100












  • great solution. I used foreach to do the same but this one is more elegant.
    – jax
    Nov 19 '18 at 19:30


















  • great solution. I used foreach to do the same but this one is more elegant.
    – jax
    Nov 19 '18 at 19:30
















great solution. I used foreach to do the same but this one is more elegant.
– jax
Nov 19 '18 at 19:30




great solution. I used foreach to do the same but this one is more elegant.
– jax
Nov 19 '18 at 19:30













2














Using some initial data as follows



public class data
{
public int id { get; set; }
public string Variable { get; set; }
public string Value { get; set; }
public decimal Coef { get; set; }
}

var listy = new List<data>() {
new data() { id=1000, Variable="Gender", Value="Male", Coef=0m },
new data() { id=1001, Variable="Gender", Value="Female", Coef=-0.205m },
new data() { id=1009, Variable="College Code", Value="AT", Coef=-1.732m },
new data() { id=1010, Variable="College Code", Value="BU", Coef=-1.806m },
new data() { id=1011, Variable="College Code", Value="EH", Coef=-1.728m },
new data() { id=1012, Variable="College Code", Value="EN", Coef=-2.003m },
new data() { id=1013, Variable="College Code", Value="LF", Coef=-1.779m },
new data() { id=1014, Variable="College Code", Value="pp", Coef=-2.042m },
new data() { id=1015, Variable="College Code", Value="SC", Coef=-2.070m },
new data() { id=1016, Variable="College Code", Value="UC", Coef=-1.845m },
new data() { id=1017, Variable="AGI", Value="AGI N/A", Coef=0.236m },
new data() { id=1018, Variable="AGI", Value="0", Coef=-0.684m },
};


Get the distinct list of Variable fields to seed the outer dictionary, then find related items in the data source, and create an inner dictionary for those:



var b = listy
.Select(x => x.Variable)
.Distinct()
// outer dictionary, key is Variable
.ToDictionary(k => k, v =>
listy
// find items in the list with the same Variable
.Where(x => x.Variable == v)
// and create a dictionary for the Value/Coef pairs.
.ToDictionary(k2 => k2.Value, v2 => v2.Coef));


Some interactive shell output:



> b["AGI"]
Dictionary<string, decimal>(2) { { "AGI N/A", 0.236 }, { "0", -0.684 } }
> b["AGI"]["0"]
-0.684
> b["College Code"]["AT"]
-1.732
> b["College Code"]["BU"]
-1.806





share|improve this answer





















  • That is a good solution as well. Thank you.
    – jax
    Nov 19 '18 at 19:34










  • This is going to iterate the list twice which is completely unnecessary
    – juharr
    Nov 19 '18 at 20:43
















2














Using some initial data as follows



public class data
{
public int id { get; set; }
public string Variable { get; set; }
public string Value { get; set; }
public decimal Coef { get; set; }
}

var listy = new List<data>() {
new data() { id=1000, Variable="Gender", Value="Male", Coef=0m },
new data() { id=1001, Variable="Gender", Value="Female", Coef=-0.205m },
new data() { id=1009, Variable="College Code", Value="AT", Coef=-1.732m },
new data() { id=1010, Variable="College Code", Value="BU", Coef=-1.806m },
new data() { id=1011, Variable="College Code", Value="EH", Coef=-1.728m },
new data() { id=1012, Variable="College Code", Value="EN", Coef=-2.003m },
new data() { id=1013, Variable="College Code", Value="LF", Coef=-1.779m },
new data() { id=1014, Variable="College Code", Value="pp", Coef=-2.042m },
new data() { id=1015, Variable="College Code", Value="SC", Coef=-2.070m },
new data() { id=1016, Variable="College Code", Value="UC", Coef=-1.845m },
new data() { id=1017, Variable="AGI", Value="AGI N/A", Coef=0.236m },
new data() { id=1018, Variable="AGI", Value="0", Coef=-0.684m },
};


Get the distinct list of Variable fields to seed the outer dictionary, then find related items in the data source, and create an inner dictionary for those:



var b = listy
.Select(x => x.Variable)
.Distinct()
// outer dictionary, key is Variable
.ToDictionary(k => k, v =>
listy
// find items in the list with the same Variable
.Where(x => x.Variable == v)
// and create a dictionary for the Value/Coef pairs.
.ToDictionary(k2 => k2.Value, v2 => v2.Coef));


Some interactive shell output:



> b["AGI"]
Dictionary<string, decimal>(2) { { "AGI N/A", 0.236 }, { "0", -0.684 } }
> b["AGI"]["0"]
-0.684
> b["College Code"]["AT"]
-1.732
> b["College Code"]["BU"]
-1.806





share|improve this answer





















  • That is a good solution as well. Thank you.
    – jax
    Nov 19 '18 at 19:34










  • This is going to iterate the list twice which is completely unnecessary
    – juharr
    Nov 19 '18 at 20:43














2












2








2






Using some initial data as follows



public class data
{
public int id { get; set; }
public string Variable { get; set; }
public string Value { get; set; }
public decimal Coef { get; set; }
}

var listy = new List<data>() {
new data() { id=1000, Variable="Gender", Value="Male", Coef=0m },
new data() { id=1001, Variable="Gender", Value="Female", Coef=-0.205m },
new data() { id=1009, Variable="College Code", Value="AT", Coef=-1.732m },
new data() { id=1010, Variable="College Code", Value="BU", Coef=-1.806m },
new data() { id=1011, Variable="College Code", Value="EH", Coef=-1.728m },
new data() { id=1012, Variable="College Code", Value="EN", Coef=-2.003m },
new data() { id=1013, Variable="College Code", Value="LF", Coef=-1.779m },
new data() { id=1014, Variable="College Code", Value="pp", Coef=-2.042m },
new data() { id=1015, Variable="College Code", Value="SC", Coef=-2.070m },
new data() { id=1016, Variable="College Code", Value="UC", Coef=-1.845m },
new data() { id=1017, Variable="AGI", Value="AGI N/A", Coef=0.236m },
new data() { id=1018, Variable="AGI", Value="0", Coef=-0.684m },
};


Get the distinct list of Variable fields to seed the outer dictionary, then find related items in the data source, and create an inner dictionary for those:



var b = listy
.Select(x => x.Variable)
.Distinct()
// outer dictionary, key is Variable
.ToDictionary(k => k, v =>
listy
// find items in the list with the same Variable
.Where(x => x.Variable == v)
// and create a dictionary for the Value/Coef pairs.
.ToDictionary(k2 => k2.Value, v2 => v2.Coef));


Some interactive shell output:



> b["AGI"]
Dictionary<string, decimal>(2) { { "AGI N/A", 0.236 }, { "0", -0.684 } }
> b["AGI"]["0"]
-0.684
> b["College Code"]["AT"]
-1.732
> b["College Code"]["BU"]
-1.806





share|improve this answer












Using some initial data as follows



public class data
{
public int id { get; set; }
public string Variable { get; set; }
public string Value { get; set; }
public decimal Coef { get; set; }
}

var listy = new List<data>() {
new data() { id=1000, Variable="Gender", Value="Male", Coef=0m },
new data() { id=1001, Variable="Gender", Value="Female", Coef=-0.205m },
new data() { id=1009, Variable="College Code", Value="AT", Coef=-1.732m },
new data() { id=1010, Variable="College Code", Value="BU", Coef=-1.806m },
new data() { id=1011, Variable="College Code", Value="EH", Coef=-1.728m },
new data() { id=1012, Variable="College Code", Value="EN", Coef=-2.003m },
new data() { id=1013, Variable="College Code", Value="LF", Coef=-1.779m },
new data() { id=1014, Variable="College Code", Value="pp", Coef=-2.042m },
new data() { id=1015, Variable="College Code", Value="SC", Coef=-2.070m },
new data() { id=1016, Variable="College Code", Value="UC", Coef=-1.845m },
new data() { id=1017, Variable="AGI", Value="AGI N/A", Coef=0.236m },
new data() { id=1018, Variable="AGI", Value="0", Coef=-0.684m },
};


Get the distinct list of Variable fields to seed the outer dictionary, then find related items in the data source, and create an inner dictionary for those:



var b = listy
.Select(x => x.Variable)
.Distinct()
// outer dictionary, key is Variable
.ToDictionary(k => k, v =>
listy
// find items in the list with the same Variable
.Where(x => x.Variable == v)
// and create a dictionary for the Value/Coef pairs.
.ToDictionary(k2 => k2.Value, v2 => v2.Coef));


Some interactive shell output:



> b["AGI"]
Dictionary<string, decimal>(2) { { "AGI N/A", 0.236 }, { "0", -0.684 } }
> b["AGI"]["0"]
-0.684
> b["College Code"]["AT"]
-1.732
> b["College Code"]["BU"]
-1.806






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 19:31









BurnsBABurnsBA

1,7241221




1,7241221












  • That is a good solution as well. Thank you.
    – jax
    Nov 19 '18 at 19:34










  • This is going to iterate the list twice which is completely unnecessary
    – juharr
    Nov 19 '18 at 20:43


















  • That is a good solution as well. Thank you.
    – jax
    Nov 19 '18 at 19:34










  • This is going to iterate the list twice which is completely unnecessary
    – juharr
    Nov 19 '18 at 20:43
















That is a good solution as well. Thank you.
– jax
Nov 19 '18 at 19:34




That is a good solution as well. Thank you.
– jax
Nov 19 '18 at 19:34












This is going to iterate the list twice which is completely unnecessary
– juharr
Nov 19 '18 at 20:43




This is going to iterate the list twice which is completely unnecessary
– juharr
Nov 19 '18 at 20:43











1














Given a collection of the date you should be able to get the results you want like this.



var lookup = data.GroupBy(x => x.Variable)
.ToDictionary(g => g.Key, g.ToDictionary(y => y.Value, y => y.Coef));


Note this will fail if you have multiple items with the same values in the Variable and Value columns.






share|improve this answer





















  • I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
    – jax
    Nov 19 '18 at 19:22
















1














Given a collection of the date you should be able to get the results you want like this.



var lookup = data.GroupBy(x => x.Variable)
.ToDictionary(g => g.Key, g.ToDictionary(y => y.Value, y => y.Coef));


Note this will fail if you have multiple items with the same values in the Variable and Value columns.






share|improve this answer





















  • I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
    – jax
    Nov 19 '18 at 19:22














1












1








1






Given a collection of the date you should be able to get the results you want like this.



var lookup = data.GroupBy(x => x.Variable)
.ToDictionary(g => g.Key, g.ToDictionary(y => y.Value, y => y.Coef));


Note this will fail if you have multiple items with the same values in the Variable and Value columns.






share|improve this answer












Given a collection of the date you should be able to get the results you want like this.



var lookup = data.GroupBy(x => x.Variable)
.ToDictionary(g => g.Key, g.ToDictionary(y => y.Value, y => y.Coef));


Note this will fail if you have multiple items with the same values in the Variable and Value columns.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 19:19









juharrjuharr

25.2k33575




25.2k33575












  • I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
    – jax
    Nov 19 '18 at 19:22


















  • I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
    – jax
    Nov 19 '18 at 19:22
















I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
– jax
Nov 19 '18 at 19:22




I do have multiple values in the Variable but not Value. That is why I want `Variable' to be the key for its sub dictionary items.
– jax
Nov 19 '18 at 19:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380866%2fc-sharp-generate-nested-dictionaries-from-the-a-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window