1

I have the following Linq,

from x in Enumerable.Range(refx - 1, 3)
from y in Enumerable.Range(refy - 1, 3)
where
  (x >= 0 && y >= 0) &&
  (x < array.GetLength(0) && y < array.GetLength(1)) &&
  !(x == refx && y == refy)
select new Location(x,y)

I would like to have the same in the other Linq format

something like,

Enumerable.Range(refx-1,3)
.Select(x)
.Range(refy - 1, 3)
.Select(y)
.Where(x >= 0 && y >= 0) &&
      (x < array.GetLength(0) && y < array.GetLength(1)) &&
      !(x == refx && y == refy)
.Select new Location(x,y)

I know the above is wrong but i'd like the first in th second format, any help is greatly appriciated

also if someone is good at linq.js converting the first to linq.js would be super great!

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272

2 Answers2

0

This would be equivalent:

    var set = Enumerable.Range(refx - 1, 3)
            .Select(x => Enumerable.Range(refy - 1, 3)
                .Where(y => (x >= 0 && y >= 0) &&
                    (x < array.GetLength(0) && y < array.GetLength(1)) &&
                    !(x == refx && y == refy))
                .Select(y => new Location(x, y)))
            .SelectMany(x => x);
Joe
  • 80,724
  • 18
  • 127
  • 145
0

I guess Zip is what you are looking for.

Enumerable.Range(refx - 1, 3).Zip(Enumerable.Range(refy - 1, 3),
    (x, y) => new Tuple<int, int>(x, y))
    .Where(t => (t.Item1 >= 0) && (t.Item2 <= 0)
    && (t.Item1 < array.GetLength(0)) && (t.Item2 < array.GetLength(1))
    && !((t.Item1 == refx) && (t.Item2 == refy)))
    .Select(t => new Location(t.Item1, t.Item2));