我想从两个方法的结果创建一个平坦的结果集,其中第一个结果是第二个的参数。
例如,方法1返回1,2,3
,我想将每个int提供给方法2,方法2每次只返回4,5,6
。
所以我希望得到像1:4, 1:5, 1:6, 2:4, 2:5, 2:6, 3:4, 3:5, 3:6
如果可能,我想在单个LINQ查询(pref c#)中执行此操作。 我希望这个解释很明确,有人可以帮助我。
编辑:
我不应该问。这很简单。对于其他需要它的人:
int[] aList = new int[] { 1, 2, 3 };
var enumerable = from a in aList
from b in GetResult(a)
select new { x = a, y = b };
答案 0 :(得分:2)
听起来你正在寻找SelectMany。
Func<IEnumerable<int>> method2 = () => new [] {4,5,6};
(new [] {1,2,3})
.SelectMany(m1Arg => method2().Select(m2arg => string.Format("{0}:{1}",m1Arg,m2arg)));
在查询语法中,它是* *中的* *,如
var q = from a in List
from b in List2
select a,b...
答案 1 :(得分:0)
使用LINQ表达式
void Main()
{
var method1 = new[] {1,2,3};
var method2 = new[] {4,5,6};
var res = from m in method1
from m2 in method2
select String.Format("{0}:{1}", m, m2);
foreach (var x in res) {
Console.Out.WriteLine(x);
}
}