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
Use 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);
}
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)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With