Search for a value in the list <Dictionary <string, object >>

I have a variable List< Dictionary < string, object >> as follows.

 private static List<Dictionary<string, object>> testData = new List<Dictionary<string, object>>(100); // Just Sample data for understanding. for (int i = 0; i < 100; i++) { var test = new Dictionary<string, object> { { "aaa", "aaa" + i % 4 }, { "bbb", "bbb" + i % 4 }, { "ccc", "ccc" + i % 4 }, { "ddd", "ddd" + i % 4 }, { "eee", "eee" + i % 4 }, { "fff", "fff" + i % 4 }, { "ggg", "ggg" + i % 4 }, { "hhh", "hhh" + i % 4 }, { "iii", "iii" + i % 4 } }; testData.Add(test); } 

I want to find the list of keys, the value in the dictionary and return List< Dictionary < string, object >> containing the passed searchPattern.

 Dictionary<string, object> searchPattern = new Dictionary<string, object>(); searchPattern .Add("aaa", "aaa4"); searchPattern .Add("eee", "eee2"); searchPattern .Add("fff", "fff1"); searchPattern .Add("ddd", "ddd3"); public List<Dictionary<string, object>> SearchList(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern) { List<Dictionary<string, object>> result; // Search the list. return result; } 

Any other search suggestions are also appreciated. Thank you very much!

+4
source share
5 answers

This will return the first dictionary in the list containing all key-value pairs in the search pattern, or null if none exist.

 public Dictionary<string, object> SearchList ( List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern ) { return testData.FirstOrDefault(x => searchPattern.All(x.Contains)); } 

If you want all matches (not just the first ones) to use Where([...]).ToList() instead of FirstOrDefault([...]) .

+1
source

Let me try:

  public static List<Dictionary<string, object>> SearchList(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern) { return testData.Where(t => { bool flag = true; foreach (KeyValuePair<string, object> p in searchPattern) { if (!t.ContainsKey(p.Key) || !t[p.Key].Equals(p.Value)) { flag = false; break; } } return flag; }).ToList(); } 

OR

  public static List<Dictionary<string, object>> SearchList(List<Dictionary<string, object>> testData, Dictionary<string, object> searchPattern) { return testData .Where(t => searchPattern.All(p => t.ContainsKey(p.Key) && t[p.Key].Equals(p.Value))) .ToList(); } 
+1
source

If I understand you correctly:

 var result = testData.Where(dic => searchPattern.All(dic.Contains)) .ToList(); 
+1
source

There are several ways to do this. _Individuals1 = file1.fileIndividuals; _Individuals2 = file2.fileIndividuals;

  foreach (KeyValuePair<int, Individual> kvpInd in _Individuals1) { foreach (KeyValuePair<int, Individual> kvpInd2 in _Individuals2) { if (kvpInd.Value.name.name == kvpInd2.Value.name.name) { similarInds.Add(kvpInd.Key, kvpInd.Value); } } } var similarInds = file1.fileIndividuals. Where(kv1 => file2.fileIndividuals.Any(kv2 => kv1.Value.name.name == kv2.Value.name.name)). ToDictionary(kv => kv.Key, kv => kv.Value); 

this will fail, if you have duplicate values ​​using linq, you can do it like this:

0
source

I agree with armen.shimoon, however something like this:

 public static List<Dictionary<string, object>> SearchList(this List<Dictionary<string, object>> list, Dictionary<string, object> searchPattern) { return list.Where(item => searchPattern.All(x => item.ContainsKey(x.Key) && x.Value.Equals(item[x.Key]) )).ToList(); } 

And then using like this:

  searchResult = testData.SearchList(searchPattern); 
0
source

Source: https://habr.com/ru/post/1433931/


All Articles