Linq帮助需要从sql模型转换为Linq对象模型

时间:2012-03-14 19:43:02

标签: javascript c# json linq linq.js

我有以下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)

我想在其他Linq格式中使用相同的

之类的,

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)

我知道上面的错误但是我想要第二种格式的第一种, 任何帮助都非常适合

如果有人擅长linq.js将第一个转换为linq.js会非常棒!

2 个答案:

答案 0 :(得分:0)

这相当于:

    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);

答案 1 :(得分:0)

我猜Zip正是你要找的。

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));