Neiro
Neiro

Reputation: 49

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?

Upvotes: 3

Views: 565

Answers (5)

Rahul
Rahul

Reputation: 77876

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();

Upvotes: 0

Access Denied
Access Denied

Reputation: 9461

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);

Upvotes: 3

John M
John M

Reputation: 2600

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.

Upvotes: 0

mahlatse
mahlatse

Reputation: 1307

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;

Upvotes: 1

Danny Goodall
Danny Goodall

Reputation: 838

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.

Upvotes: 1

Related Questions