如何使用Linq从列表中删除重复项和与重复项进行比较的另一项。以下是查询:
var test = myList
.GroupBy(x => x.WHEN)
.Select(y => y.First())
.ToList();
这里列表已经排序,并且WHEN引用DateTime。这就是我想要实现的。
01.01.2018 08:00:00 --> should be removed
01.01.2018 08:00:00 --> should be removed
01.01.2018 08:10:00
01.01.2018 08:11:00
02.01.2018 08:00:45 --> should be removed
02.01.2018 08:00:45 --> should be removed
答案 0 :(得分:3)
让我们按组大小过滤(我们只希望包含一个项的组):
var test = myList
.GroupBy(x => x.WHEN)
.Where(group => group.Count() == 1) // single item groups only
.Select(group => group.First())
.ToList();
答案 1 :(得分:1)
您可以尝试以下linq代码:
myList.GroupBy(x => x.WHEN)
.Where(x => x.Any(xEle => myList.Where(ele => ele == xEle))
.Count() == 1)
.ToList();