检查数组中的特定值

use*_*996 0 c# linq arrays lambda

我有2个数组,我在下面比较.

int[] values = { 1, 2, 3, 4, etc };

int[,] limits = { { 1, 2 }, { 2, 5 }, { 2, 6 },etc };
Run Code Online (Sandbox Code Playgroud)

我可以非常容易地比较数组的所有元素,或者如果数组中的特定元素是顺序的,则使用以下代码查看它们是否都在相应的限制内,

 if (Enumerable.Range(0, values.Length).All(x => values[x] >= limits[x, 0] && values[x] <= limits[x, 1]))
 {
    //Do something            
 };
Run Code Online (Sandbox Code Playgroud)

但是,如果我只想检查数组中的特定非顺序缺陷,例如索引0,4,6和9,我该怎么做?预先感谢.

juh*_*arr 5

只需替换Enumerable.Range为要检查的索引集合即可

new[] {0, 4, 6, 9}.All(x => values[x] >= limits[x, 0] && values[x] <= limits[x, 1])
Run Code Online (Sandbox Code Playgroud)