从获取列表与其中唯一值;字典&LT;字符串,字符串&GT;&GT; [英] Get Unique values from List<Dictionary<string, string>>
问题描述
我有名单,其中,字典&LT;字符串,字符串&GT;&gt;在它的一些DATAS
对象。
/* 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 */
我想获得不同的值(名单,其中,串&GT;
)的字典,其中最关键的是为CCC,并在关键的BBB的价值等于bbbValue1
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".
预期成果:
返回一个字符串列表包含字典中值,其中关键是等于CCC的关键BBB的值等于bbbValue1中的名单,其中,字典&LT;字符串,字符串&GT; &GT;
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>>
.
推荐答案
我想你想要的:
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()
这篇关于从获取列表与其中唯一值;字典&LT;字符串,字符串&GT;&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!