I am trying to get multiple count stats from a table but I'm not getting what I want.
Code
var result = _db.Users.Select(g => new
{
count = _db.Users.Count(),
acCount = _db.Users.Count(u => u.User.State == AccountState.AwaitingConfirmation)
});
Sql request
SELECT (
SELECT COUNT(*)
FROM `users` AS `c`
) AS `count`, (
SELECT COUNT(*)
FROM `users` AS `u`
INNER JOIN `users` AS `u.User` ON `u`.`UserId` = `u.User`.`Id`
WHERE `u.User`.`State` = 4
) AS `acCount`
FROM `users` AS `g`
Expected Result
result = { count = ?, acCount = ? }
Actual Result (4 users in the table)
result = [
{ count = ?, acCount = ? },
{ count = ?, acCount = ? },
{ count = ?, acCount = ? },
{ count = ?, acCount = ? }
]
Obviously this is wrong because I am using Select
which gives me the counts N times (N being the number of rows in the users table).
What I want is to be able to get these counts directly and once, preferably in one request.
I also tried GroupBy(i => 1)
but that gives A LOT of sql requests..
Thank you.
count
value to the previous one?