Hi
We are having an issue where LLBLGen generates an inner join for a LINQ or statement. The code in question can be seen below:
var isRoleOwner = metadata.Role
.Any(r =>
r.TenantId == tenantId &&
r.IsActive &&
(
r.OwnerUserId == CurrentUserId ||
r.SecondaryOwners.Any(x => x.UserId == CurrentUserId)
));
This resulted in the following SQL being generated:
SELECT TOP(@p6)
CASE WHEN EXISTS
(
SELECT [LPA_L2].[VisibleForSelectionByManagers]
FROM ([ServiceManager].[dbo].[Role] [LPA_L2]
INNER JOIN [ServiceManager].[dbo].[RoleSecondaryOwner] [LPA_R3] ON [LPA_L2].[RoleId]=[LPA_R3].[RoleId])
WHERE ( ( ( ( [LPA_L2].[TenantId] = @p1) AND ( [LPA_L2].[IsActive] = @p2))
AND ( ( [LPA_L2].[OwnerUserId] = @p3) OR
EXISTS (
SELECT [LPLA_2].[WhenChangedAD]
FROM [ServiceManager].[dbo].[User] [LPLA_2]
WHERE ( [LPLA_2].[UserId] = [LPA_R3].[UserId] AND ( [LPLA_2].[UserId] = @p4))))))
) THEN 1 ELSE 0 END AS [LPFA_1]
FROM [ServiceManager].[dbo].[Role] [LPLA_1]
This returns false for users that are Owners and not SecondaryOwners since SecondaryOwners is empty.
As a workaround we rewrote the query to this:
isRoleOwner = metadata.Role
.Any(r =>
r.TenantId == tenantId &&
r.IsActive &&
(
r.OwnerUserId == CurrentUserId ||
metadata.RoleSecondaryOwner.Any(ros => ros.RoleId == r.RoleId && ros.UserId == CurrentUserId)
));
This resulted in the following SQL being generated:
SELECT TOP(@p6)
CASE WHEN EXISTS (
SELECT [LPLA_1].[VisibleForSelectionByManagers]
FROM [ServiceManager].[dbo].[Role] [LPLA_1]
WHERE ( ( ( ( [LPLA_1].[TenantId] = @p1) AND ( [LPLA_1].[IsActive] = @p2)) AND ( ( [LPLA_1].[OwnerUserId] = @p3)
OR EXISTS (
SELECT [LPLA_2].[UserId]
FROM [ServiceManager].[dbo].[RoleSecondaryOwner] [LPLA_2]
WHERE ( ( ( [LPLA_2].[RoleId] = [LPLA_1].[RoleId]) AND ( [LPLA_2].[UserId] = @p4)))))))
) THEN 1 ELSE 0 END AS [LPFA_1]
FROM [ServiceManager].[dbo].[Role] [LPLA_1]
This returns true for users that are owners or secondary owners.
LLBLGEN Pro version: 5.6 (5.6.3) RTM Build date: 18-mai-2020
LLBLGEN Pro runtime library version: 5.6.0
Template group: Adapter .Net 4.7.2
Database: Microsoft SQL Server 2017, Provider: SD.LLBLGen.Pro.DQE.SqlServer 5.6.0