简体   繁体   English

C#,LINQ。 如何在元素组中查找元素

[英]C#, LINQ. How to find an element within group of elements

Imagine you have int[] data = new int [] { 1, 2, 1, 1, 3, 2 } 想象一下你有int[] data = new int [] { 1, 2, 1, 1, 3, 2 }

I need sub-array with only those which conform to a condition data[i] > data[i-1] && data[i] > data[i + 1] ... ie I need all items which stick over their immediate neighbours. 我只需要那些符合条件data[i] > data[i-1] && data[i] > data[i + 1]子数组...即我需要所有坚持其近邻的项目。

From example above I should get { 2, 3 } 从上面的例子我应该得到{ 2, 3 }

Can it be done in LINQ? 可以在LINQ中完成吗?

Thanks 谢谢

data.Where((val, index)=>(index == 0 || val > data[index - 1]) 
                      && (index == data.Length - 1 || val > data[index + 1]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM