I would like to execute the Linq to Sql statement, which captures the quantity and average value in a (filtered) dataset. I have work, but the database requires two queries, when it is possible in one query.
Interestingly, I can get one request that will be emitted when I use the group by clause.
For instance:
select count(*), avg(duration) from events
My linq looks like this:
var x = from e in db.events
select e;
x = from i in x
where i.NAME == "foo"
select i;
return new {
count = x.Count(),
avgDuration = x.Average(e => e.Duration)
};
With this code, I get two requests:
SELECT AVG([t0].[DURATION]) AS [value] FROM [dbo].[EVENTS] AS [t0]
and
SELECT COUNT(*) AS [value] FROM [dbo].[EVENTS] AS [t0]
Is there another way?
Gavin source
share