在LINQ中查询数组的位置
我有数组号= {2,3,4,5,6}
现在我必须从表“dtlRecord”中选择这个数字是列的行。
Number count 2 10 3 23 4 20
所以我需要的是
select sum(count) from dtlRecord where number in (2,3,4,5,6) group by number
我需要LINQ to SQL中的上述查询
如何使用内连接:
int[] numbers = new[] { 2, 3, 4, 5, 6 };
from r in dtlRecord
join number in numbers on r.Number equals number
group r by r.Number into g
select new {
Number = g.Key,
Sum = g.Sum(r => r.Count)
};
要么
dtlRecord
.Join(numbers, r => r.Number, number => number, (r, number) => new {r, number})
.GroupBy(arg => arg.r.Number, arg => arg.r)
.Select(g => new
{
Number = g.Key,
Sum = g.Sum(r => r.Count)
});
使用Enumerable.Contains
:
int[] numbers = new[] { 2, 3, 4, 5, 6 };
var query = from r in dtlRecords
where numbers.Contains(r.Number)
group r by r.Number into g
select new {
Number = g.Key,
Sum = g.Sum(r => r.Count)
};
foreach(var result in query) {
Console.WriteLine("Number = {0}, Sum = {1}", result.Number, result.Sum);
}
LINQ等价于哪里
http://www.onedotnetway.com/linq-equivalent-of-where-in/