计算2个数组的数量差
本文关键字:2个 数组 计算 | 更新日期: 2023-09-27 18:25:41
对于A和B中的每个数字,如果它们的差值大于3,则添加新的数字
A={6,7,10}
B={2,3}
result={4,5,4,8,7}
例如:
6-2=4真加4
6-3=3错误
7-2=5真加5
我的尝试:
var result = A.Select((a, i) => new
{
desired = a - B[i] > 3 ? a - B[i] :0
});
听起来你想要:
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);
}
}
}
var query = from a in A
from b in B
where (a - b) > 3
select a - b;