Search for a value in List<Dictionary<string, object>>

21,948

Solution 1

This will return the first dictionary in the list that contains all the key-value pairs in search pattern, or null if there isn't one.

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 the matches (not just the first one) use Where([...]).ToList() instead of FirstOrDefault([...]).

Solution 2

If I understand you correctly:

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

Solution 3

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();
    }
Share:
21,948
Ramesh Durai
Author by

Ramesh Durai

..

Updated on September 13, 2020

Comments

  • Ramesh Durai
    Ramesh Durai over 3 years

    I have a List< Dictionary < string, object >> variable 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 search for a list of key,value in the Dictionary and return the List< Dictionary < string, object >> contains the searchPattern I passed.

    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 suggestions to search also appreciated. Many Thanks!!

  • horgh
    horgh over 11 years
    Don't you think that this answer has already been mentioned earlier?...and not even once
  • Ramesh Durai
    Ramesh Durai over 11 years
    Thanks! It's Working like a charm :)
  • Ramesh Durai
    Ramesh Durai over 11 years
    I'm expecting the result of type List<Dictionary<string, object>> but it's returning IEnumerable<KeyValuePair<string, object>>.
  • cuongle
    cuongle over 11 years
    @RameshDurai: Okay, what's data in each dictionary in result you need?
  • verdesmarald
    verdesmarald over 11 years
    @RameshDurai Is there a reason you unaccepted this answer? As far as I can tell it does the same thing as the other answer you accepted afterwards, but is cleaner?
  • Ramesh Durai
    Ramesh Durai over 11 years
    Yes. Your answer was working. But It's returned only First value, I expected all datas. Where([...]).ToList() should work.
  • Ramesh Durai
    Ramesh Durai over 11 years
    Please check the answers of veredesmarald and Konstantin Vasilcov.
  • verdesmarald
    verdesmarald over 11 years
    @RameshDurai I used FirstOrDefault because your comment says "I want to search for the row which contains all the key value pairs [...] and return that alone"... alone != a list of things.
  • Ramesh Durai
    Ramesh Durai over 11 years
    I apologize for my mistake. In the question I mentioned clearly.
  • Ramesh Durai
    Ramesh Durai over 11 years
    Sorry to unaccept your answer. Since @veredesmarald answer also working and he is the first one to answer I accepted his. Hope you will agree.
  • cuongle
    cuongle over 11 years
    @RameshDurai: I still don't understand what result you expect? folowing veredesmarald, the result is Zero?
  • Ramesh Durai
    Ramesh Durai over 11 years