我有一個列表Dictionary<string, object>
。 如何按值查找所有字典中的重複值?如何在List <Dictionary <string,object >>中查找重複值?
1
A
回答
1
緊湊代碼使用LINQ:
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
list.SelectMany(dictionary => dictionary.Values).GroupBy(d => d).Where(x => x.Count() >1);
+1
這隻適用於一本字典。但沒有列表。 – Sebi
+0
看起來像他編輯它 - 檢查出來 –
1
經典老循環
var uniqueValues = new HashSet<object>();
var duplicateValues = new List<object>();
foreach (var value in yourDictionaries.SelectMany(dict => dict.Values))
{
if (uniqueValues.Add(value) == false)
{
duplicateValues.Add(value);
}
}
SelectMany
是獲取所有詞典的所有值的重要手段。
如果你是LINQ的球迷則可以通過使用Aggregate
或GroupBy
1
你可以找到使用LINQ它們的出現重複值,將其轉換成LINQ表達式的例子。 它給你重複的值和它的出現(列表中的索引和字典中的鍵)。
var duplicates = dicList
.SelectMany((x, i) => x.Select(p => new { Index = i, Key = p.Key, Value = p.Value }))
.GroupBy(x => x.Value)
.Where(x => x.Count() > 1)
.Select(x => new
{
Value = x.First().Value,
Occurrences = x.Select(o => new { Index = o.Index, Key = o.Key })
});
如果你只是想重複的值,則使用簡化版
var duplicates = listOfDic
.SelectMany(x => x.Values)
.GroupBy(x => x)
.Where(x => x.Count() > 1);
相關問題
- 1. 如何修改List中的值<Dictionary <string,object >>
- 2. C#從Dictionary獲取值<string,List <object>>
- 3. List <Map <String,String >> vs List <Object>
- 4. Sort Map <String,List <Object[]>>
- 5. C#如何通過list.count命令dictionary <string,list <string>>?
- 6. 演員表<Object> to List <Map <String,Object >>
- 7. 從字典<int,List <Tuple <string,object >>>到Dictionary <int,List <Tuple <string,object,AnEnum >>> with LINQ
- 8. 如何將Dictionary <string,List <object>>轉換成C#中的DataTable?
- 9. 如何將C#中的數據錶轉換爲Dictionary <string,list <object>>?
- 10. Dictionary <string,List <int>>複製到另一個詞典
- 11. 如何將List <Dictionary <string,object >>轉換爲List <[具有動態屬性的新類]>
- 12. XML使用Dictionary <string,List <string>>對象
- 13. 將DataGrid綁定到List <Dictionary <string,string >>
- 14. 如何將List <Class>轉換成C#中的Dictionary <string,List <String>>?
- 15. C#將Dictionary <string,AnyType>轉換爲Dictionary <string,Object>(涉及反射)
- 16. Json反序列化形式Dictionary <string,Dictionary <string,string >>
- 17. 泛型:將`List <String>`添加到`List <Object>`
- 18. 從List <Map <String,Object >>創建HashMap <String,Map>給出java.lang.ClassCastException
- 19. Post Map <String,List <String>>
- 20. 如何正確聯合HashMap <String,ArrayList <Object>>和HashMap <String,Object>
- 21. 如何爲Map <String,List <Object>>和MultivaluedMap <String,Object>定義一個通用方法?
- 22. For-Each Loop,Map <String,Map <String,List <String> >>
- 23. Javapoet - TypeName - HashMap <String,HashMap <String,List <String> >> generation?
- 24. 從ObservableCollection類型的集合中查詢<Dictionary <string,object >>
- 25. WPF綁定Dictionary <string,List <string>要listView,ListBox怎麼樣?
- 26. Dictionary <string,object>如何知道值的類型?
- 27. Dictionary <string,string> vs Hashtable <string,string>
- 28. IDictionary <String,List <OpenXmlCompositeElement >> - 獲取List <OpenXmlCompositeElement>?
- 29. List <Map <String,List <>>> in BaseAdapter,Android
- 30. Swagger:<string,Object>
的可能的複製[C#:從字典中刪除重複值(http://stackoverflow.com/questions/1462101/c -remove-duplicate-values-from-dictionary) –
posible duplicate of http://stackoverflow.com/questions/7172394/finding-duplicate-values-in-dictionary-and-print-key-of-the-duplicate-element –
他要求在「字典」的「列表」中查找重複項。這些不是完全重複的。 –