0

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

List<int> lifeField;
I want to find index of those members, that correspond to next condition:
(((number % (10 * currentMove)) == 1) || ((number % (10 * currentMove)) == 2))
where number is member of List and currentMove is integer - 0,1,2

例如:

if it is first move
currentMove = 0
I search number 1 or 2 
if it is second move 
I search numbers 11,21

......我想在这里找到如何用 lambda.... 或其他函数写这个

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

3 回答 3

4
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();
于 2013-01-12T10:54:40.893 回答
0

FindIndex接受 a Predicatewhich 是一个接受元素并返回 a 的表达式bool

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

number => (
  ((number % (10 * currentMove)) == 1)
  ||
  ((number % (10 * currentMove)) == 2)
)
于 2013-01-12T10:52:36.553 回答
0

注意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);
于 2013-01-12T10:54:38.350 回答