Click here to Skip to main content
15,748,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a list of Dictionaries something like below
C#
List<Dictionary<string,string>


I would like to fetch all the dictionaries which have a key value pair in common.

Ex. Assume that I have a key as Customer. I would like to fetch all the dictionaries from the list where the customer key has a value of 10.

Thanks in Advance
Gopi
Posted
Comments
OriginalGriff 18-Nov-13 4:42am    
Please do not post the same question twice: if you need to make changes, use the "Improve question" widget instead.
I have deleted the older version.
Gopi Kishan Mariyala 18-Nov-13 4:43am    
Hey Sorry I'm not sure how the old version came into picture. Thanks for deleting the older version
OriginalGriff 18-Nov-13 4:53am    
No problem!

1 solution

C#
private List<Dictionary<string, string>> GetDictionaries(List<Dictionary<string, string>> dictionaries, string key, string value) {
   List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
   foreach (Dictionary<string, string> dic in dictionaries) {
      if (dic.ContainsKey(key) && dic[key].Equals(value)) {
         result.Add(dic);
      }
   }
   return result;
}


This should get you near what you need.
Hope this helps.
 
Share this answer
 
v2
Comments
Gopi Kishan Mariyala 18-Nov-13 4:54am    
It is very much near to what I need. I will let you know if I need any change. Thanks
phil.o 18-Nov-13 5:08am    
I updated the solution with some improvements : passing the initial dictionary as a paramater, and some basic checking. Please see my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900