我试图找到一个 LINQ 表达式来比较两个列表元素。
我想做的是:
List<int> _int = new List<int> { 1, 2, 3, 3, 4, 5};
_int.Where(x => x == _int[(_int.IndexOf(x)) + 1]);
不幸的是,只有最后一个 +1
跳出了列表的范围。
如何在 LINQ 表达式中将列表中的一项与其下一项进行比较?
最佳答案
不是很好,但应该可以。
list.Where((index, item) => index < list.Count - 1 && list[index + 1] == item)
或以下内容。
list.Take(list.Count - 1).Where((index, item) => list[index + 1] == item)
关于c# - 使用 LINQ 比较两个列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3102661/