我有一个序列。例如:
new [] { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 }
现在我必须在不改变整体顺序的情况下删除重复的值。对于上面的序列:
new [] { 10, 1, 5, 25, 45, 40, 100, 1, 2, 3 }
如何使用 LINQ 做到这一点?
最佳答案
var list = new List<int> { 10, 1, 1, 5, 25, 45, 45, 45, 40, 100, 1, 1, 2, 2, 3 };
List<int> result = list.Where((x, index) =>
{
return index == 0 || x != list.ElementAt(index - 1) ? true : false;
}).ToList();
这会返回你想要的。希望对您有所帮助。
关于c# - 如何使用 LINQ 对序列中的相同值进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766072/