Where in Query with Array in LINQ
I have array number = {2,3,4,5,6}
Now i have to select rows from table "dtlRecord" where this number is a column.
Number count 2 10 3 23 4 20
So what i need is
select sum(count) from dtlRecord where number in (2,3,4,5,6) group by number
I need above query in LINQ to SQL
How about just using inner join:
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)
};
or
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 Equivalent Of Where IN
http://www.onedotnetway.com/linq-equivalent-of-where-in/
上一篇: 更改urwid / python2.6中当前显示的列表框的内容
下一篇: 在LINQ中查询数组的位置