Say I have a summary table like this:
AcitivityCache
(
pkId (bigint, PK),
Date (DateTime),
OfficeId (int, FK)
TransactionCount (int),
RejectCount (int),
InvitationCount (int),
RequestCount (int)
...
--more counts here--
...
)
Now I need to fetch the total of the counts over a time period (no grouping by office, just sums) which I can do with SQL like this:
SELECT SUM(TransactionCount) as TotalTxCount, SUM(RejectCount) as TotalRejectCount, ...
FROM ActivityCache
WHERE [Date] between '2011-02-01' and '2012-02-01'
How can I achieve that with linq?
Most solutions I have seen so far have some grouping. But for this case, I need no grouping, just the totals. I can do like this:
DateTime dateFrom, toDate; // initialized later
...
var q = dbContext.ActivityCaches
.Where(a=> a.Date > fromDate && a.Date <= toDate)
.GroupBy(a=> 1)
.Select(g=> new
{
TotalTransactionCount = g.Sum(a => a.TransactionCount),
TotalRejectCount = g.Sum(a => a.RejectCount),
...
...
});
Or, I can fetch the columns first, then take sum programmatically.
Or, I can use an SP (dont like in this case).
Any better ideas (without group by)?
P.S: I wont be able to change the db schema
I don’t think LINQ to Entities has some nice query for this situation.
Another solution is using using Entity SQL (ESQL)