Hi,
v5.6.1, SQL Server
I have the following inheritance:
Entities
Company
User
CompanyUser (has CompanyId)
so some users are regular users and some have a company. I'd like to fetch users with optional company name and id (left join). I do see that when User is fetched the run-time does a left join to the CompanyUser table automatically, that makes sense as I'm querying the base User. I thought I could cheat my way around and do something like:
var q = qf.Create().From(qf.User.LeftJoin(CompanyUserEntity.Relations.CompanyEntityUsingCompanyId))
however, in this case the left join becomes an inner join (besides multi-part identifier errors which probably can be fixed with aliasing):
SELECT TOP(@p2) [LPA_L2].[Id] AS [CId],
[LPA_L1].[Name],
[XYZ].[dbo].[Users].[Id],
[XYZ].[dbo].[Users].[FirstName],
[XYZ].[dbo].[Users].[LastName],
[XYZ].[dbo].[Users].[Email]
FROM (((([XYZ].[dbo].[Entities] [LPA_L3]
INNER JOIN [XYZ].[dbo].[Users] [LPA_L4]
ON [LPA_L3].[Id] = [LPA_L4].[Id])
INNER JOIN [XYZ].[dbo].[CompanyUsers] [LPA_L5]
ON [LPA_L4].[Id] = [LPA_L5].[Id])
LEFT JOIN [XYZ].[dbo].[Companies] [LPA_L2]
ON [LPA_L2].[Id] = [LPA_L5].[CompanyId])
LEFT JOIN [XYZ].[dbo].[Entities] [LPA_L1]
ON [LPA_L1].[Id] = [LPA_L2].[Id])
so far came up with:
var q = qf.Create().From(qf.User.LeftJoin(qf.CompanyUser).On(CompanyUserFields.Id == UserFields.Id).LeftJoin(CompanyUserEntity.Relations.CompanyEntityUsingCompanyId))
which turns into:
SELECT TOP(100 /* @p2 */) [LPA_L7].[Id] AS [CId],
[LPA_L6].[Name],
[LPA_L2].[Id],
[LPA_L2].[FirstName],
[LPA_L2].[LastName],
[LPA_L2].[Email]
FROM (((((([XYZ].[dbo].[Entities] [LPA_L1]
INNER JOIN [XYZ].[dbo].[Users] [LPA_L2]
ON [LPA_L1].[Id] = [LPA_L2].[Id])
LEFT JOIN [XYZ].[dbo].[CompanyUsers] [LPA_L5]
ON [LPA_L5].[Id] = [LPA_L2].[Id])
LEFT JOIN [XYZ].[dbo].[Users] [LPA_L4]
ON [LPA_L4].[Id] = [LPA_L5].[Id])
LEFT JOIN [XYZ].[dbo].[Entities] [LPA_L3]
ON [LPA_L3].[Id] = [LPA_L4].[Id])
LEFT JOIN [XYZ].[dbo].[Companies] [LPA_L7]
ON [LPA_L7].[Id] = [LPA_L5].[CompanyId])
LEFT JOIN [XYZ].[dbo].[Entities] [LPA_L6]
ON [LPA_L6].[Id] = [LPA_L7].[Id])
CORRECTION: changed inner to left
Is it possible to rework the query so there's no additional two left joins which I don't really need for this query results, but I'd suspect they are being added due to inheritance for the CompanyUser itself. To this:
SELECT TOP(100 /* @p2 */) [LPA_L7].[Id] AS [CId],
[LPA_L6].[Name],
[LPA_L2].[Id],
[LPA_L2].[FirstName],
[LPA_L2].[LastName],
[LPA_L2].[Email]
FROM (((([XYZ].[dbo].[Entities] [LPA_L1]
INNER JOIN [XYZ].[dbo].[Users] [LPA_L2]
ON [LPA_L1].[Id] = [LPA_L2].[Id])
LEFT JOIN [XYZ].[dbo].[CompanyUsers] [LPA_L5]
ON [LPA_L5].[Id] = [LPA_L2].[Id])
LEFT JOIN [XYZ].[dbo].[Companies] [LPA_L7]
ON [LPA_L7].[Id] = [LPA_L5].[CompanyId])
LEFT JOIN [XYZ].[dbo].[Entities] [LPA_L6]
ON [LPA_L6].[Id] = [LPA_L7].[Id])
Curious if there's a good way to optimize that.
Thank you!