我有一个Dictionary<String, List<String>>
,我需要对其进行过滤,并仅保留List<String>
包含重复(重复)值的那些对。我知道应该有一个linq子查询,但是我所有的变体都失败了。
示例:
var dictionary = new Dictionary<String,List<String>>();
dictionary.Add("Key1", new List<String>{"1","2","2","3"});
dictionary.Add("Key2", new List<String>{"1","2","3"});
dictionary.Add("Key3", new List<String>{"1","2","4"});
dictionary.Add("Key4", new List<String>{"1","2","5"});
所以我只需要保留第一对,因为该列表具有重复的值“ 2”。
答案 0 :(得分:3)
词典中的
代码:
var filteredDictionary = dictionary.Where(pair => pair.Value.Distinct().Count()
!= pair.Value.Count)
.ToDictionary(x => x.Key, x => x.Value);