I currently get 2 aggregate function results this way:
var fooCount = ctx.Foo.Count();
var barCount = ctx.Bar.Count();
This produces 2 different SQL queries. I am looking for a way to combine these into one query.
In plain SQL I can combine these two queries this way:
SELECT
(SELECT COUNT(*) FROM Foo) AS FooCount,
(SELECT COUNT(*) FROM Bar) AS BarCount
How can we do this using Entity Framework? All I have found was for multiple aggregate function in the same table. But in my case it are different tables.
I tried something like this with no success:
var query = from together in (new
{
FooCount = db.Foo.Count(),
BarCount = db.Bar.Count()
}) select together; //<-- visual studio let me not write "select together;"
IEnumerable, why notnew int[] {ctx.Foo.Count(), ctx.Bar.Count()}Countis going to create a separate SQL call and the OP wants to combine them into one.