对于A中的每个数字和B中的每个数字(如果它们的差异大于3),请添加新数字
A={6,7,10}
B={2,3}
result={4,5,4,8,7}
例如:
6-2 = 4 true加4
6-3 = 3假
7-2 = 5 true加5
我的尝试:
var result = A.Select((a, i) => new
{
desired = a - B[i] > 3 ? a - B[i] :0
});
答案 0 :(得分:6)
var query = from a in A
from b in B
where (a - b) > 3
select a - b;
答案 1 :(得分:6)
这听起来像你想要的:
var query = from a in A
from b in B
let diff = a - b
where diff > 3
select diff;
或者用点符号表示,它真的很整齐:
var query = A.SelectMany(a => B, (a, b) => a - b)
.Where(x => x > 3);
使用您的测试数据:
using System;
using System.Linq;
class Test
{
public static void Main()
{
int[] A = {6, 7, 10};
int[] B = {2, 3};
var query = A.SelectMany(a => B, (a, b) => a - b)
.Where(x => x > 3);
foreach (var result in query)
{
Console.WriteLine(result);
}
}
}