尼斯,干净的交叉只使用扩展方法Linq中加入 [英] Nice, clean cross join in Linq using only extension methods
问题描述
可能重复:结果
的嵌套“从”的LINQ查询与扩展方法
表示块引用>
我敢肯定,这个以前被要求,但老实说,我无法找到任何
我很好奇相当于语法会是怎样以下仅使用内置的LINQ扩展方法:
VAR Z1 = $ b $从X b在XS
从Ÿ在YS
选择新的{X,Y };
我能得到与此相同的结果:
VAR Z2 = xs.SelectMany(X => ys.Select(Y =>新建{X,Y}));
但它产生不同的IL代码,代码是有点令人费解,很难理解。 ?是否有一个更清洁的方式使用扩展方法来做到这一点。
下面是为写我的整个测试方法:
私人无效测试()
{
变种XS =新[] {1D,2D,3D};
变种YS =新[] {4D,5D,6D};从X在XS
变种Z1 =
在YS
从Ÿ选择新的{X,Y};
VAR Z2 = xs.SelectMany(X => ys.Select(Y =>新建{X,Y}));
}
这里是:IL代码 XS =新的双[]
{
1.0,
2.0,
3.0
};
双[] =伊苏新的双[]
{
4.0,
5.0,
6.0
};
变种Z = $ B $从X在XS $从Y B $ B在YS
B选择新的
{
X = X,$ B $通过= Y
};
VAR Z2 = xs.SelectMany((双X)=>从YS
Y形
选择新的
{
X = X,$ B $通过= Y
});
}
解决方案一个办法是:
VAR Z2 = xs.SelectMany(X => YS,(X,Y)=>新建{X,Y} );
Possible Duplicate:
Nested “from” LINQ query expressed with extension methodsI'm sure this has been asked before, but I honestly couldn't find anything.
I'm curious what the equivalent syntax would be for the following using only built-in Linq extension methods:
var z1 = from x in xs from y in ys select new { x, y };
I can get the same results with this:
var z2 = xs.SelectMany(x => ys.Select(y => new { x, y }));
But it produces different IL code, and the code is a bit convoluted and hard to understand. Is there a cleaner way to do this with extension methods?
Here's my entire test method as written:
private void Test() { var xs = new[] { 1D, 2D, 3D }; var ys = new[] { 4D, 5D, 6D }; var z1 = from x in xs from y in ys select new { x, y }; var z2 = xs.SelectMany(x => ys.Select(y => new { x, y })); }
Here's the [Edit: C# interp of the] IL code (using ILSpy):
private void Test() { double[] xs = new double[] { 1.0, 2.0, 3.0 }; double[] ys = new double[] { 4.0, 5.0, 6.0 }; var z = from x in xs from y in ys select new { x = x, y = y }; var z2 = xs.SelectMany((double x) => from y in ys select new { x = x, y = y }); }
解决方案One way would be:
var z2 = xs.SelectMany(x => ys, (x, y) => new {x, y});
这篇关于尼斯,干净的交叉只使用扩展方法Linq中加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!