LINQ - dictionary in dictionary
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
add a comment |
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
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
add a comment |
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
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
c# .net linq
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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);
Thank you very much. Your answer turned out to be true.
– Victor Akhremenka
Nov 20 '18 at 12:34
add a comment |
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.
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:53
add a comment |
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;
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:36
add a comment |
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.
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:37
add a comment |
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();
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:37
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
Thank you very much. Your answer turned out to be true.
– Victor Akhremenka
Nov 20 '18 at 12:34
add a comment |
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);
Thank you very much. Your answer turned out to be true.
– Victor Akhremenka
Nov 20 '18 at 12:34
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:53
add a comment |
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.
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:53
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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;
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:36
add a comment |
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;
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:36
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:37
add a comment |
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.
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:37
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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();
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:37
add a comment |
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();
Surprised that so many good people. Thank)
– Victor Akhremenka
Nov 20 '18 at 12:37
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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