Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Unique values from List<Dictionary<string, string>>

I have List<Dictionary<string, string>> object with some datas in it.

/* Values in the list will be like
   [0] - 
         aaa - aaaValue1   (Key, Value)
         bbb - bbbValue1
         ccc - cccValue1
         ddd - dddValue1 
   [1] - 
         aaa - aaaValue2   (Key, Value)
         bbb - bbbValue2
         ccc - cccValue2
         ddd - dddValue2 

    and so on */

I want to get the distinct values( List<string> ) in the dictionary where the key is equal to "ccc" and the value of the key "bbb" is equal to "bbbValue1".

Expected Result:

Return a string list contains the dictionary value where key is equal to "ccc" and the value of the key "bbb" is equal to "bbbValue1" in the List<Dictionary<string, string>>.

like image 794
Ramesh Durai Avatar asked Jan 31 '26 07:01

Ramesh Durai


1 Answers

I think you want:

var result = testData.Where(dict => dict.ContainsKey("EmpNo"))
                     .Select(dict => dict["EmpNo"])
                     .Distinct()
                     .ToList();

or if you want the result as a set:

var result = new HashSet<string>(from dict in testData       
                                 where dict.ContainsKey("EmpNo")        
                                 select dict["EmpNo"]);        

EDIT: You've changed your question completely, which isn't a nice thing to do (ask a new one instead), but to answer it in its current state:

var result = testData.Where(dict => dict.ContainsKey("ccc") 
                                 && dict.ContainsKey("bbb")
                                 && dict["bbb"] == "bbbValue1")
                     .Select(dict => dict["ccc"])
                     .Distinct()
                     .ToList()
like image 154
Ani Avatar answered Feb 01 '26 22:02

Ani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!