How to remove duplicates from a list in C#

送分小仙女□ 提交于 2019-12-10 13:19:06

问题


I want to remove duplicates from this list:

List<Dictionary<string, object>> val = new List<Dictionary<string, object>>();

It does not work if I apply Distinct() in this way:

 List<Dictionary<string, object>> result = val.Distinct().ToList<Dictionary<string, object>>()

Update: Problem is now solved. I used the MySQL union command to read table from the database.


回答1:


Try this:

List<Dictionary<string, object>> result = val.Distinct(new myDictionaryComparer()).ToList();

where myDictionaryComparer is a Comparer Class. You can implement your comparison logic in this class.




回答2:


You can use the following method:

val.Distinct()


来源:https://stackoverflow.com/questions/2756458/how-to-remove-duplicates-from-a-list-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!