Background
Right now I have a system named FoorBar where there are Users, Rooms, and Authorizations.
Detailed Background
A user can have many authorizations and many rooms can be added to a single authorization. To handle this I have the following tables: FooBar_User
, FooBar_UserAuth
, FooBar_Authorization
, FoorBar_AuthType
, FooBar_RoomAuth
, and Room_Inventory
.
I need a query to return the authorization id, the number of rooms a user has, the authorization type, and the expiration date of the authorization.
SQL
The SQL to generate the desired output is below:
SELECT a.AuthId
,COUNT(ra.RoomAuthId) AS RmCnt
,fat.AuthTypeAbbr
,a.ExpirationDate
FROM FooBar_User AS u
INNER JOIN FooBar_UserAuth AS ua ON u.UserId = ua.UserId
INNER JOIN FooBar_Authorization AS a ON ua.AuthId = a.AuthId
INNER JOIN FooBar_AuthType AS fat ON a.AuthTypeId = fat.AuthTypeId
LEFT JOIN FooBar_RoomAuth AS ra ON a.AuthId = ra.AuthId AND ra.IsActive = 1
LEFT JOIN Room_Inventory AS ri ON ra.RmId = ri.RMID AND ri.IsActive = 1
WHERE a.IsActive = 1
AND u.IsActive = 1
AND u.UserId = 10
GROUP BY a.AuthId, fat.AuthTypeAbbr, a.ExpirationDate
LINQ Query
from ua in db.FooBar_UserAuth
join ra in db.FooBar_RoomAuth
on new { ua.FooBar_Authorization.AuthId, IsActive = true }
equals new { ra.AuthId, ra.IsActive } into raJoin
from ra in raJoin.DefaultIfEmpty()
join ri in db.Room_Inventory
on new { ra.RmId, IsActive = true }
equals new { RmId = ri.RMID, ri.IsActive } into riJoin
from ri in riJoin.DefaultIfEmpty()
where
ua.FooBar_Authorization.IsActive &&
ua.IsPi == true &&
ua.FooBar_User.IsActive &&
ua.FooBar_User.UserId == 10
group new { ua.FooBar_Authorization, ua.FooBar_Authorization.FooBar_AuthType, ra } by new
{
AuthId = (int?)ua.FooBar_Authorization.AuthId,
ua.FooBar_Authorization.FooBar_AuthType.AuthTypeAbbr,
ua.FooBar_Authorization.ExpirationDate
} into g
select new
{
AuthId = g.Key.AuthId.Value,
RmCnt = g.Count(p => p.ra.RoomAuthId != 0),
AuthType = g.Key.AuthTypeAbbr,
ExpirationDate = g.Key.ExpirationDate
}
LINQPad and Linqer Output
AuthId RmCnt AuthType ExpirationDate
30 0 Type1 12/31/2020
31 0 Type2 12/31/2020
Problem
The issue is that this LINQ query works perfectly when it is run in both Linqer and LINQPad. The output is exactly as desired. However, when it is run in C# with ASP.NET, MVC, and Entity Framework, the RmCnt will never be 0
. Instead, it will be 1
. No matter what is changed that seems to be the case. One other user asked a similar question but I can't figure out how to apply their solution with my query.
Any help would be appreciated.