如何在 C# List <T> LINQ 中同时查找重复项和原始项?
How to find both duplicates and original in a C# List <T> LINQ?
我在这里阅读了几乎所有关于在 C# 中查找列表类型字符串中的重复项的答案,但我还没有真正找到我需要的解决方案。
假设我有一个列表:
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"};
我想到的第一个方法是:
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();
我在这里阅读了几乎所有关于在 C# 中查找列表类型字符串中的重复项的答案,但我还没有真正找到我需要的解决方案。
假设我有一个列表:
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"};
我想到的第一个方法是:
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();