从List <Dictionary <string,string >>获取唯一值

Ram*_*rai 2 .net c# linq dictionary list

我有List<Dictionary<string, string>>一些数据对象.

/* 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 */
Run Code Online (Sandbox Code Playgroud)

我想List<string>在字典中获得不同的值(),其中键等于"ccc",键"bbb"的值等于"bbbValue1".

预期结果:

返回一个字符串列表,其中包含字符值,其中键等于"ccc",键"bbb"的值等于"bbbValue1" List<Dictionary<string, string>>.

Ani*_*Ani 9

我想你想要:

var result = testData.Where(dict => dict.ContainsKey("EmpNo"))
                     .Select(dict => dict["EmpNo"])
                     .Distinct()
                     .ToList();
Run Code Online (Sandbox Code Playgroud)

或者如果你想把结果作为一组:

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

编辑:你已经完全改变了你的问题,这不是一件好事(要求换一个新问题),而是以当前状态回答:

var result = testData.Where(dict => dict.ContainsKey("ccc") 
                                 && dict.ContainsKey("bbb")
                                 && dict["bbb"] == "bbbValue1")
                     .Select(dict => dict["ccc"])
                     .Distinct()
                     .ToList()
Run Code Online (Sandbox Code Playgroud)