我试图找到一个 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)