开发者

LINQ: Get the data from the Dictionary in c#

开发者 https://www.devze.com 2023-02-01 18:00 出处:网络
I have a Dictionary> in c#: Dictionary<string, List<string>> l_dictRawData = new Dictionary<string, List<string>> {

I have a Dictionary> in c#:

Dictionary<string, List<string>> l_dictRawData =
    new Dictionary<string, List<string>> {
        { "TamilNadu", new List<string>{ "Chennai", "Madurai" }},
        { "Andhra", new List<string>{"Hyderabad", "Secundarabad" }},
        { "Karnataka",  new List<string>{"mysore", "Bangalore" }}
    };

Then I have the InputList:

List<string> l_lstIn开发者_JAVA百科put = new List<string> { "Hyderabad", "Secundarabad" };

The result will be the (i.e) if the dictionary l_dictRawData contains both "Hyderabad" and "Secundarabad" ,then select the key value:

string l_strOutPut = "Andhra"; 

Here is my code :

   var Query = from l_strData in l_dictRawData
               from l_strItem in l_lstInput    
               where l_strData .Value.Contains(l_strItem )   
             select new { CityName = l_strItem, 
                          StateName = l_strData.Key       
                  }; 

How can i get the ouput using LINQ in c#


Do you know that the list's data will be in the same order as the dictionary value's order? If so:

var result = l_dictRawData.Where(pair => pair.Value.SequenceEqual(l_lstInput))
                          .Select(pair => pair.Key)
                          .FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号