我在这里阅读了几乎所有关于在C#中查找List类型字符串中的重复项的答案,但我还没找到真正需要的解决方案。
假设我有一个清单:
List<String> list = new List<String>{"6","1","2","4","6","5","1","2","1","2"};
我想要检索的是重复值及其原始文件,在新列表中,以便生成的列表如下:
List<String> duplist = {"6","6","1","1","1,","2","2","2"};
答案 0 :(得分:9)
我想到的第一种方法是:
List<String> duplist = list
.GroupBy(str => str)
.Where(g=> g.Count() != 1)
.SelectMany(g => g)
.ToList();
如果有人有兴趣,这是查询语法:
var dups = from str in list
group str by str into g
where g.Count() != 1
from str in g
select str;
List<String> duplist = dups.ToList();