gpt4 book ai didi

c# - 使用 LINQ 比较两个列表元素

转载 作者:太空狗 更新时间:2023-10-29 22:17:45 26 4
gpt4 key购买 nike

我试图找到一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com