Search

Using DataLoadOptions with a Linq To Sql Inheritance Hierarchy results in multiple SQL LEFT JOINS by Orlin Todorov

Closed
as Won't Fix Help for as Won't Fix

3
0
Sign in
to vote
Type: Bug
ID: 361683
Opened: 8/13/2008 11:26:45 AM
Access Restriction: Public
1
Workaround(s)
0
User(s) can reproduce this bug
Assume we have an inheritance hierarchy with base class Customer and derived class CustomerDerived. Customer has a One-to-One association with Address.

The following code results in multiple LEFT JOIN statements being generated in the SQL sent to the database server. The number of LEFT JOINS equals the number of classes in the inheritance hierarchy.

                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Customer>(c => c.Addresses);
                db.LoadOptions = dlo;

                var q = from c in db.Customers select c;

Below is the resulting SQL generated by Linq to Sql:
SELECT [t0].[CustomerTypeId], [t0].[CustomerId], [t0].[Name], [t2].[test], [t2].[AddressId], [t2].[CustomerId] AS [CustomerId2], [t2].[Street], [t4].[test] AS [test2], [t4].[AddressId] AS [AddressId2], [t4].[CustomerId] AS [CustomerId3], [t4].[Street] AS [Street2]
FROM [dbo].[Customer] AS [t0]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t1].[AddressId], [t1].[CustomerId], [t1].[Street]
    FROM [dbo].[Address] AS [t1]
    ) AS [t2] ON [t2].[CustomerId] = [t0].[CustomerId]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t3].[AddressId], [t3].[CustomerId], [t3].[Street]
    FROM [dbo].[Address] AS [t3]
    ) AS [t4] ON [t4].[CustomerId] = [t0].[CustomerId]

Note the two JOINS and that "1 AS test" appearing in the projection?

Now consider a real-life scenario where we have 5 classes in the hierarchy and 5 associated tables. This results in 25 JOINs and a huge projection.
Details (expand)
Product Language
English

Version

Visual Studio 2008 Service Pack 1 Beta
Operating System
Windows XP Professional
Operating System Language
English
Steps to Reproduce
- Unzip the attached project.
- Execute the file TestLinqDatabase.sql to create the test database.
- Open TestLinq.csproj in Visual Studio 2008 SP1.
- Change the connection string in Properties/Settings and app.config to point to the appropriate SQL server.
- Debug the console application. Step through the code and note the SQL expression generated by the last statement (by hovering over the variable q with the mouse).
Actual Results
SELECT [t0].[CustomerTypeId], [t0].[CustomerId], [t0].[Name], [t2].[test], [t2].[AddressId], [t2].[CustomerId] AS [CustomerId2], [t2].[Street], [t4].[test] AS [test2], [t4].[AddressId] AS [AddressId2], [t4].[CustomerId] AS [CustomerId3], [t4].[Street] AS [Street2]
FROM [dbo].[Customer] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[AddressId], [t1].[CustomerId], [t1].[Street]
FROM [dbo].[Address] AS [t1]
) AS [t2] ON [t2].[CustomerId] = [t0].[CustomerId]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t3].[AddressId], [t3].[CustomerId], [t3].[Street]
FROM [dbo].[Address] AS [t3]
) AS [t4] ON [t4].[CustomerId] = [t0].[CustomerId]
Expected Results
SELECT [t0].[CustomerTypeId], [t0].[CustomerId], [t0].[Name], [t2].[AddressId], [t2].[CustomerId] AS [CustomerId2], [t2].[Street]
FROM [dbo].[Customer] AS [t0]
LEFT OUTER JOIN (
SELECT [t1].[AddressId], [t1].[CustomerId], [t1].[Street]
FROM [dbo].[Address] AS [t1]
) AS [t2] ON [t2].[CustomerId] = [t0].[CustomerId]
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
File Name Submitted By Submitted On File Size  
TestLinq.zip (restricted) 8/21/2008 -
Sign in to post a comment.
Posted by CecilPhillip on 6/16/2010 at 11:22 AM
Was this fixed in .NET 4.0 ?
Posted by 1AA8628F-1191-4D4A-A45E-4DCB35CA00B4 on 3/31/2010 at 1:51 PM
How is it that you can say you confirmed this major bug , but will not fix it?
A lot companies have invested heavily into Lts and it is bad business to leave us hanging like this.
I know you want to force everyone to EF, but you have to support therproducts you own and have a user base invested in.
Please reposond.
thanks.
Posted by Microsoft on 2/25/2009 at 4:43 PM
Hi,

Thank you for taking the time to send this feedback and bug report. We have reviewed the issue and confirmed the behavior, but we will not be fixing this in the next release of LINQ to SQL.

LINQ to SQL Team
Posted by Microsoft on 11/13/2008 at 11:32 AM
Hi Orlin,

Thank you for reporting this issue. We have confirmed it is a bug, and we are investigating a fix for a future version of LINQ to SQL.

Thanks,
LINQ to SQL Team
Posted by Orlin Todorov on 8/25/2008 at 11:39 AM
I have attached a sample project file demonstrating the problem as requested. Please advise on the status of this bug report.
Posted by Microsoft on 8/14/2008 at 12:56 AM
Thanks for reporting the issue.
In order to fix the issue, we must first reproduce the issue in our labs. We are unable to reproduce the issue with the steps you provided.

It may help if you provide us with:
a zipped project file

It would be greatly appreciated if you could provide us with that information as quickly as possible. If we do not hear back from you within 7 days, we will close this issue


Thanks again for your efforts and we look forward to hearing from you.
Visual Studio Product Team
Sign in to post a workaround.
Posted by Damien Guard on 5/21/2010 at 2:28 PM
It is possible to work around this bug using an extension method to do a per-query include as shown at http://damieng.com/blog/2010/05/21/include-for-linq-to-sql-and-maybe-other-providers

[)amien