0

I have the below dictionary

        Dictionary<string,string> q1=new Dictionary<string,string>
        { 
        {"h1","name1"},{"h2","name2"}
        };
        Dictionary<string,string> q2=new Dictionary<string,string>
        { 
        {"h1","name12"},{"h2","name23"}
        };
        Dictionary<string,string> q3=new Dictionary<string,string>
        { 
        {"h1","name123"},{"h2","name234"}
        };
        List<Dictionary<string,string>> m1 = new List<Dictionary<string,string>> { q1,q2,q3 };
        Dictionary<string, List<Dictionary<string,string>>> mhi = new Dictionary<string, List<Dictionary<string,string>>>();
        mhi.Add("x1", m1);

I need to return a list which has the values name1,name12,name123 using linq. I am aware of normal method which works for me. But I am curious to know how to implement this using linq

1 Answer 1

1

Try this:

var q1 = new Dictionary<string, string> {
    {"h1", "name1"},
    {"h2", "name2"}
};
var q2 = new Dictionary<string, string> {
    {"h1", "name12"},
    {"h2", "name23"}
};
var q3 = new Dictionary<string, string> {
    {"h1", "name123"},
    {"h2", "name234"}
};
var m1 = new List<Dictionary<string, string>> { q1, q2, q3 };
//Using LINQ
List<string> result = (from dictionary in m1
                       from keyValuePair in dictionary
                       where keyValuePair.Key == "h1" 
                       select keyValuePair.Value).ToList();
//result = name1,name12,name123

//without linq
var result2 = new List<string>();
foreach(var dictionary in m1)
    foreach(var keyValuePair in dictionary)
        if(keyValuePair.Key == "h1")
            result2.Add(keyValuePair.Value);

Edit:
The from clause specifies the data source, the where clause applies the filter, and the select clause projects each element of the sequence into a new form.

Linq queries are not executed until we iterate through it. (Here .ToList() is doing that). It's like a blueprint which specifies how the information is returned when executed(iterated).

Lets examine each statement separately:

  1. from dictionary in m1 - This is much like foreach(var dictionary in m) except that it doesn't iterate (Because its a blueprint). It specifies which source we are iterating through (m1) and the variable to assign to each member (dictionary that is. We know that it will be of type Dictionary<String, String>)
  2. from keyValuePair in dictionary - Here we use the dictionary variable created from the previous statement. The type of keyValuePair will be KeyValuePair<string,string> because we will be "iterating" through a Dictionary<string,string> when the query is executed.
  3. where keyvaluePair.Key == "h1" - This filters out the keyValuePairs from the previous statement whose Key property equals "h1".
  4. Now that we filtered out the KeyValuePairs, we can select their Value property. This "projects" the filtered out KeyValuePair sequence to the new type IEnumerable<string>
  5. Finally, ToList method executes the query to get the results.
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent that works.. Appreciate if you can provide some explanation.
Also in the abpve code I forgot to add this statement Dictionary<string, List<Dictionary<string,string>>> mhi = new Dictionary<string, List<Dictionary<string,string>>>(); How does the linq query looks in this case mhi.Add("x1", m1);
I have added more information. Hope it helps you :)
About mhi, how will it be a query? Its not a query. You are creating a dictionary and adding some value ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.