LINQ - dictionary in dictionary












3















I'm just learning how to program, and therefore I didn't really understand LINQ.
I have:



Dictionary<string, Dictionary<string, string>> dirData = new Dictionary<string, Dictionary<string, string>>
{
{
"key1", new Dictionary<string, string>
{
{"subKey1", "value546" },
{"subKey2", "value412" },
{"subKey3", "value100" },
{"subKey4", "value27" }
}
},
{
"key2", new Dictionary<string, string>
{
{"subKey1", "value27" },
{"subKey2", "value98" },
{"subKey3", "value100" }
}
},
{
"key3", new Dictionary<string, string>
{
{"subKey1", "value29" },
{"subKey2", "value202" },
{"subKey3", "value22" },
{"subKey5", "value1" },
{"subKey6", "value3" }
}
}
};


I need to return the Dictionary<string, Dictionary <string, string >> where subkey == "subKey3" with the value value == "value100".



How can this be organized using LINQ?










share|improve this question

























  • There seems to be two dictionaries with subkey == 3 and value == value100, are you looking for two dictionaries at the end?

    – mahlatse
    Nov 20 '18 at 11:43
















3















I'm just learning how to program, and therefore I didn't really understand LINQ.
I have:



Dictionary<string, Dictionary<string, string>> dirData = new Dictionary<string, Dictionary<string, string>>
{
{
"key1", new Dictionary<string, string>
{
{"subKey1", "value546" },
{"subKey2", "value412" },
{"subKey3", "value100" },
{"subKey4", "value27" }
}
},
{
"key2", new Dictionary<string, string>
{
{"subKey1", "value27" },
{"subKey2", "value98" },
{"subKey3", "value100" }
}
},
{
"key3", new Dictionary<string, string>
{
{"subKey1", "value29" },
{"subKey2", "value202" },
{"subKey3", "value22" },
{"subKey5", "value1" },
{"subKey6", "value3" }
}
}
};


I need to return the Dictionary<string, Dictionary <string, string >> where subkey == "subKey3" with the value value == "value100".



How can this be organized using LINQ?










share|improve this question

























  • There seems to be two dictionaries with subkey == 3 and value == value100, are you looking for two dictionaries at the end?

    – mahlatse
    Nov 20 '18 at 11:43














3












3








3








I'm just learning how to program, and therefore I didn't really understand LINQ.
I have:



Dictionary<string, Dictionary<string, string>> dirData = new Dictionary<string, Dictionary<string, string>>
{
{
"key1", new Dictionary<string, string>
{
{"subKey1", "value546" },
{"subKey2", "value412" },
{"subKey3", "value100" },
{"subKey4", "value27" }
}
},
{
"key2", new Dictionary<string, string>
{
{"subKey1", "value27" },
{"subKey2", "value98" },
{"subKey3", "value100" }
}
},
{
"key3", new Dictionary<string, string>
{
{"subKey1", "value29" },
{"subKey2", "value202" },
{"subKey3", "value22" },
{"subKey5", "value1" },
{"subKey6", "value3" }
}
}
};


I need to return the Dictionary<string, Dictionary <string, string >> where subkey == "subKey3" with the value value == "value100".



How can this be organized using LINQ?










share|improve this question
















I'm just learning how to program, and therefore I didn't really understand LINQ.
I have:



Dictionary<string, Dictionary<string, string>> dirData = new Dictionary<string, Dictionary<string, string>>
{
{
"key1", new Dictionary<string, string>
{
{"subKey1", "value546" },
{"subKey2", "value412" },
{"subKey3", "value100" },
{"subKey4", "value27" }
}
},
{
"key2", new Dictionary<string, string>
{
{"subKey1", "value27" },
{"subKey2", "value98" },
{"subKey3", "value100" }
}
},
{
"key3", new Dictionary<string, string>
{
{"subKey1", "value29" },
{"subKey2", "value202" },
{"subKey3", "value22" },
{"subKey5", "value1" },
{"subKey6", "value3" }
}
}
};


I need to return the Dictionary<string, Dictionary <string, string >> where subkey == "subKey3" with the value value == "value100".



How can this be organized using LINQ?







c# .net linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 11:43









Tolga Evcimen

4,11493970




4,11493970










asked Nov 20 '18 at 11:34









Victor AkhremenkaVictor Akhremenka

183




183













  • There seems to be two dictionaries with subkey == 3 and value == value100, are you looking for two dictionaries at the end?

    – mahlatse
    Nov 20 '18 at 11:43



















  • There seems to be two dictionaries with subkey == 3 and value == value100, are you looking for two dictionaries at the end?

    – mahlatse
    Nov 20 '18 at 11:43

















There seems to be two dictionaries with subkey == 3 and value == value100, are you looking for two dictionaries at the end?

– mahlatse
Nov 20 '18 at 11:43





There seems to be two dictionaries with subkey == 3 and value == value100, are you looking for two dictionaries at the end?

– mahlatse
Nov 20 '18 at 11:43












5 Answers
5






active

oldest

votes


















3














You can use the following code snippet, there are two of them in your sample BTW:



var result = dirData.Values.Where(d => d.ContainsKey("subKey3") && d["subKey3"] == "value100");


Update




I need to return the Dictionary<string, Dictionary <string, string >>
where subkey == "subKey3" with the value value == "value100".



Get Dictionary of Dictionaries:



Dictionary<string,Dictionary<string,string>> result = dirData.Where(d => d.Value.ContainsKey("subKey3") && d.Value["subKey3"] == "value100").ToDictionary(k=>k.Key,v=>v.Value);





share|improve this answer


























  • Thank you very much. Your answer turned out to be true.

    – Victor Akhremenka
    Nov 20 '18 at 12:34



















1














Something along the lines of



var vals = dirData.Where(x => x.Value.Keys.Contains("subKey1") && x.Value.Values.Contains(("value29")));


should work. I just tested it using vals.Count() and got the number 1 returning.



Also, just as a heads up: there are two missing commas in your sub-dictionaries :)



Edit: I think that the answer by @Access Denied actually is probably better. Just leaving mine as an alternative.






share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:53



















1














Just complicating it a bit, you can also use Linq-object this way



  var test =  from x in dirData
where x.Value.Any(m => m.Key == "subKey3" && m.Value == "value100")
select x;





share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:36



















0














Here's another way:



List<Dictionary<string, string>> result = dirData.Where(w => w.Value["subKey3"] == "value100").Select(s => s.Value).ToList();


I'm presuming that more than one of the dictionaries can match the condition based on your sample data, therefore this statement returns a list of dictionaries. If you only expect one match you should replace the ToList() with Single()/SingleOrDefault() as appropriate.






share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37



















0














probably something like



var data = dirData.Where(d => d.Value.Any(x => x.Key == "subKey3" && x.Value == "value100")).ToList();


if you are looking for only those entries where key = "subKey3" and value = "value100" then probably use SelectMany() like



var data = dirData.SelectMany(x => x.Value).Where(x => x.Key == "subKey3" && x.Value == "value100").ToList();





share|improve this answer


























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37











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%2f53392118%2flinq-dictionary-in-dictionary%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can use the following code snippet, there are two of them in your sample BTW:



var result = dirData.Values.Where(d => d.ContainsKey("subKey3") && d["subKey3"] == "value100");


Update




I need to return the Dictionary<string, Dictionary <string, string >>
where subkey == "subKey3" with the value value == "value100".



Get Dictionary of Dictionaries:



Dictionary<string,Dictionary<string,string>> result = dirData.Where(d => d.Value.ContainsKey("subKey3") && d.Value["subKey3"] == "value100").ToDictionary(k=>k.Key,v=>v.Value);





share|improve this answer


























  • Thank you very much. Your answer turned out to be true.

    – Victor Akhremenka
    Nov 20 '18 at 12:34
















3














You can use the following code snippet, there are two of them in your sample BTW:



var result = dirData.Values.Where(d => d.ContainsKey("subKey3") && d["subKey3"] == "value100");


Update




I need to return the Dictionary<string, Dictionary <string, string >>
where subkey == "subKey3" with the value value == "value100".



Get Dictionary of Dictionaries:



Dictionary<string,Dictionary<string,string>> result = dirData.Where(d => d.Value.ContainsKey("subKey3") && d.Value["subKey3"] == "value100").ToDictionary(k=>k.Key,v=>v.Value);





share|improve this answer


























  • Thank you very much. Your answer turned out to be true.

    – Victor Akhremenka
    Nov 20 '18 at 12:34














3












3








3







You can use the following code snippet, there are two of them in your sample BTW:



var result = dirData.Values.Where(d => d.ContainsKey("subKey3") && d["subKey3"] == "value100");


Update




I need to return the Dictionary<string, Dictionary <string, string >>
where subkey == "subKey3" with the value value == "value100".



Get Dictionary of Dictionaries:



Dictionary<string,Dictionary<string,string>> result = dirData.Where(d => d.Value.ContainsKey("subKey3") && d.Value["subKey3"] == "value100").ToDictionary(k=>k.Key,v=>v.Value);





share|improve this answer















You can use the following code snippet, there are two of them in your sample BTW:



var result = dirData.Values.Where(d => d.ContainsKey("subKey3") && d["subKey3"] == "value100");


Update




I need to return the Dictionary<string, Dictionary <string, string >>
where subkey == "subKey3" with the value value == "value100".



Get Dictionary of Dictionaries:



Dictionary<string,Dictionary<string,string>> result = dirData.Where(d => d.Value.ContainsKey("subKey3") && d.Value["subKey3"] == "value100").ToDictionary(k=>k.Key,v=>v.Value);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 11:50

























answered Nov 20 '18 at 11:41









Access DeniedAccess Denied

5,14121643




5,14121643













  • Thank you very much. Your answer turned out to be true.

    – Victor Akhremenka
    Nov 20 '18 at 12:34



















  • Thank you very much. Your answer turned out to be true.

    – Victor Akhremenka
    Nov 20 '18 at 12:34

















Thank you very much. Your answer turned out to be true.

– Victor Akhremenka
Nov 20 '18 at 12:34





Thank you very much. Your answer turned out to be true.

– Victor Akhremenka
Nov 20 '18 at 12:34













1














Something along the lines of



var vals = dirData.Where(x => x.Value.Keys.Contains("subKey1") && x.Value.Values.Contains(("value29")));


should work. I just tested it using vals.Count() and got the number 1 returning.



Also, just as a heads up: there are two missing commas in your sub-dictionaries :)



Edit: I think that the answer by @Access Denied actually is probably better. Just leaving mine as an alternative.






share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:53
















1














Something along the lines of



var vals = dirData.Where(x => x.Value.Keys.Contains("subKey1") && x.Value.Values.Contains(("value29")));


should work. I just tested it using vals.Count() and got the number 1 returning.



Also, just as a heads up: there are two missing commas in your sub-dictionaries :)



Edit: I think that the answer by @Access Denied actually is probably better. Just leaving mine as an alternative.






share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:53














1












1








1







Something along the lines of



var vals = dirData.Where(x => x.Value.Keys.Contains("subKey1") && x.Value.Values.Contains(("value29")));


should work. I just tested it using vals.Count() and got the number 1 returning.



Also, just as a heads up: there are two missing commas in your sub-dictionaries :)



Edit: I think that the answer by @Access Denied actually is probably better. Just leaving mine as an alternative.






share|improve this answer













Something along the lines of



var vals = dirData.Where(x => x.Value.Keys.Contains("subKey1") && x.Value.Values.Contains(("value29")));


should work. I just tested it using vals.Count() and got the number 1 returning.



Also, just as a heads up: there are two missing commas in your sub-dictionaries :)



Edit: I think that the answer by @Access Denied actually is probably better. Just leaving mine as an alternative.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 11:41









Danny GoodallDanny Goodall

193322




193322













  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:53



















  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:53

















Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:53





Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:53











1














Just complicating it a bit, you can also use Linq-object this way



  var test =  from x in dirData
where x.Value.Any(m => m.Key == "subKey3" && m.Value == "value100")
select x;





share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:36
















1














Just complicating it a bit, you can also use Linq-object this way



  var test =  from x in dirData
where x.Value.Any(m => m.Key == "subKey3" && m.Value == "value100")
select x;





share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:36














1












1








1







Just complicating it a bit, you can also use Linq-object this way



  var test =  from x in dirData
where x.Value.Any(m => m.Key == "subKey3" && m.Value == "value100")
select x;





share|improve this answer













Just complicating it a bit, you can also use Linq-object this way



  var test =  from x in dirData
where x.Value.Any(m => m.Key == "subKey3" && m.Value == "value100")
select x;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 11:45









mahlatsemahlatse

1,014518




1,014518













  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:36



















  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:36

















Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:36





Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:36











0














Here's another way:



List<Dictionary<string, string>> result = dirData.Where(w => w.Value["subKey3"] == "value100").Select(s => s.Value).ToList();


I'm presuming that more than one of the dictionaries can match the condition based on your sample data, therefore this statement returns a list of dictionaries. If you only expect one match you should replace the ToList() with Single()/SingleOrDefault() as appropriate.






share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37
















0














Here's another way:



List<Dictionary<string, string>> result = dirData.Where(w => w.Value["subKey3"] == "value100").Select(s => s.Value).ToList();


I'm presuming that more than one of the dictionaries can match the condition based on your sample data, therefore this statement returns a list of dictionaries. If you only expect one match you should replace the ToList() with Single()/SingleOrDefault() as appropriate.






share|improve this answer
























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37














0












0








0







Here's another way:



List<Dictionary<string, string>> result = dirData.Where(w => w.Value["subKey3"] == "value100").Select(s => s.Value).ToList();


I'm presuming that more than one of the dictionaries can match the condition based on your sample data, therefore this statement returns a list of dictionaries. If you only expect one match you should replace the ToList() with Single()/SingleOrDefault() as appropriate.






share|improve this answer













Here's another way:



List<Dictionary<string, string>> result = dirData.Where(w => w.Value["subKey3"] == "value100").Select(s => s.Value).ToList();


I'm presuming that more than one of the dictionaries can match the condition based on your sample data, therefore this statement returns a list of dictionaries. If you only expect one match you should replace the ToList() with Single()/SingleOrDefault() as appropriate.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 11:46









John MJohn M

1,21941220




1,21941220













  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37



















  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37

















Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:37





Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:37











0














probably something like



var data = dirData.Where(d => d.Value.Any(x => x.Key == "subKey3" && x.Value == "value100")).ToList();


if you are looking for only those entries where key = "subKey3" and value = "value100" then probably use SelectMany() like



var data = dirData.SelectMany(x => x.Value).Where(x => x.Key == "subKey3" && x.Value == "value100").ToList();





share|improve this answer


























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37
















0














probably something like



var data = dirData.Where(d => d.Value.Any(x => x.Key == "subKey3" && x.Value == "value100")).ToList();


if you are looking for only those entries where key = "subKey3" and value = "value100" then probably use SelectMany() like



var data = dirData.SelectMany(x => x.Value).Where(x => x.Key == "subKey3" && x.Value == "value100").ToList();





share|improve this answer


























  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37














0












0








0







probably something like



var data = dirData.Where(d => d.Value.Any(x => x.Key == "subKey3" && x.Value == "value100")).ToList();


if you are looking for only those entries where key = "subKey3" and value = "value100" then probably use SelectMany() like



var data = dirData.SelectMany(x => x.Value).Where(x => x.Key == "subKey3" && x.Value == "value100").ToList();





share|improve this answer















probably something like



var data = dirData.Where(d => d.Value.Any(x => x.Key == "subKey3" && x.Value == "value100")).ToList();


if you are looking for only those entries where key = "subKey3" and value = "value100" then probably use SelectMany() like



var data = dirData.SelectMany(x => x.Value).Where(x => x.Key == "subKey3" && x.Value == "value100").ToList();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 12:00

























answered Nov 20 '18 at 11:52









RahulRahul

62.7k124482




62.7k124482













  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37



















  • Surprised that so many good people. Thank)

    – Victor Akhremenka
    Nov 20 '18 at 12:37

















Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:37





Surprised that so many good people. Thank)

– Victor Akhremenka
Nov 20 '18 at 12:37


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392118%2flinq-dictionary-in-dictionary%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)