Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在有条件的列表中查找数字?_C#_Linq_Lambda - Fatal编程技术网

C# 如何在有条件的列表中查找数字?

C# 如何在有条件的列表中查找数字?,c#,linq,lambda,C#,Linq,Lambda,我是编写lambda表达式的新手,我有下一个问题: 我有 。。。。。。 我想知道如何在这里用lambda写这个。。。。或在其他功能中 int indexOfLife = Array.FindIndex(lifeField, ?????????); FindIndex接受一个谓词,该谓词是一个表达式,它接受一个元素并返回一个bool 所以,你想要这样的东西: number => ( ((number % (10 * currentMove)) == 1) || ((number

我是编写lambda表达式的新手,我有下一个问题: 我有

。。。。。。 我想知道如何在这里用lambda写这个。。。。或在其他功能中

int indexOfLife = Array.FindIndex(lifeField, ?????????);

FindIndex
接受一个
谓词
,该谓词是一个表达式,它接受一个元素并返回一个
bool

所以,你想要这样的东西:

number => (
  ((number % (10 * currentMove)) == 1)
  ||
  ((number % (10 * currentMove)) == 2)
)

FindIndex
接受一个
谓词
,该谓词是一个表达式,它接受一个元素并返回一个
bool

所以,你想要这样的东西:

number => (
  ((number % (10 * currentMove)) == 1)
  ||
  ((number % (10 * currentMove)) == 2)
)

请注意,currentMove不能为0,它将导致
DivideByZeroException

  var ints = Enumerable.Range(1,100).ToArray();
  int currentMove = 1;
  var output = ints.Where(number => number % (10 * currentMove) == 1 || (number % (10 * currentMove))==2);

请注意,currentMove不能为0,它将导致
DivideByZeroException

  var ints = Enumerable.Range(1,100).ToArray();
  int currentMove = 1;
  var output = ints.Where(number => number % (10 * currentMove) == 1 || (number % (10 * currentMove))==2);

当我尝试使用它作为谓词时,它仍然会给我异常。当我尝试使用它作为谓词时,它仍然会给我异常。这会返回所有索引的列表,看起来非常好。谢谢。这是所有索引的返回列表,看起来很好。非常感谢。
var indexes = lifeField.Select((x,i) => new {Index = i, Element = x})
    .Where(x => 
        ((x.Element % (10 * currentMove)) == 1) || 
        ((x.Element % (10 * currentMove)) == 2))
    .Select(x => x.Index)
    .ToList();