Hi,
I have a question about a SQL statement generated by a LINQ2SQL query. I have two database tables (VisibleForDepartmentId
is a foreign key):
AssignableObject Department
---------------------- ------------
AssignableObjectId ┌────> DepartmentId
AssignableObjectType │
VisibleForDepartmentId ───┘
And the following mapping information (note that AssignableObject
is abstract):
<Database Name="SO_755661" Class="DataClassesDataContext">
<Table Name="dbo.AssignableObject" Member="AssignableObjects">
<Type Name="AssignableObject" Modifier="Abstract">
<Column Name="AssignableObjectId" Type="System.Int32"
DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true"
IsDbGenerated="true" CanBeNull="false" />
<Column Name="AssignableObjectType" Type="System.String"
DbType="VarChar(50) NOT NULL" CanBeNull="false"
AccessModifier="Private" IsDiscriminator="true"/>
<Column Name="VisibleForDepartmentId" Type="System.Int32"
DbType="Int" CanBeNull="true" />
<Association Name="Department_AssignableObject" Member="VisibleForDepartment"
ThisKey="VisibleForDepartmentId" OtherKey="DepartmentId"
Type="Department" IsForeignKey="true" />
<Type Name="Asset" InheritanceCode="Asset" IsInheritanceDefault="true" />
<Type Name="Role" InheritanceCode="Role" />
</Type>
</Table>
<Table Name="dbo.Department" Member="Departments">
<Type Name="Department">
<Column Name="DepartmentId" Type="System.Int32"
DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true"
IsDbGenerated="true" CanBeNull="false" />
<Column Name="Name" Type="System.String" DbType="VarChar(50) NOT NULL"
CanBeNull="false" />
<Association Name="Department_AssignableObject" Member="AssignableObjects"
ThisKey="DepartmentId" OtherKey="VisibleForDepartmentId"
Type="AssignableObject" />
</Type>
</Table>
</Database>
And the following code:
var loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Asset>(a => a.VisibleForDepartment);
dataContext.LoadOptions = loadOptions;
var assets = from a in dataContext.Assets
select a;
This results in a SQL query with two identical left outer joins:
SELECT t0.AssignableObjectType, t0.AssignableObjectId, t0.VisibleForDepartmentId,
t2.test, t2.DepartmentId, t2.Name, t4.test AS test2,
t4.DepartmentId AS DepartmentId2, t4.Name AS Name2
FROM dbo.AssignableObject AS t0
LEFT OUTER JOIN (
SELECT 1 AS test, t1.DepartmentId, t1.Name
FROM dbo.Department AS t1
) AS t2 ON t2.DepartmentId = t0.VisibleForDepartmentId
LEFT OUTER JOIN (
SELECT 1 AS test, t3.DepartmentId, t3.Name
FROM dbo.Department AS t3
) AS t4 ON t4.DepartmentId = t0.VisibleForDepartmentId
Why are there two outer joins where one would have been sufficient?
Kind regards,
Ronald