Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to Entities excessive joins in generated SQL
    primarykey
    data
    text
    <p>I have a .NET Entity Data Model set up with relationships so that I don't need to manually join entities in my LINQ queries. The LINQ query below references another table <strong>CustomerUserField</strong>:</p> <pre><code>from c in Customer.GetCustomer(this.ClientId, intRecordId) select new { c.TitleId, c.FirstName, c.LastName, c.Phone, c.MobilePhone, c.Fax, c.EmailAddress, c.CustomerUserField.Text1, c.CustomerUserField.Text2, c.CustomerUserField.Text3, c.CustomerUserField.Text4, c.CustomerUserField.Text5 }; </code></pre> <p>It looks nice &amp; neat in C#, however the generated SQL creates a separate left outer join for every column in the referenced table:</p> <pre><code>SELECT [Limit1].[C1] AS [C1], [Limit1].[TitleId] AS [TitleId], ... FROM [dbo].[Customer] AS [Extent1] LEFT OUTER JOIN [dbo].[CustomerUserField] AS [Extent2] ON [Extent1].[CustomerId] = [Extent2].[CustomerId] LEFT OUTER JOIN [dbo].[CustomerUserField] AS [Extent9] ON [Extent2].[CustomerUserFieldId] = [Extent9].[CustomerUserFieldId] LEFT OUTER JOIN [dbo].[CustomerUserField] AS [Extent10] ON [Extent2].[CustomerUserFieldId] = [Extent10].[CustomerUserFieldId] LEFT OUTER JOIN [dbo].[CustomerUserField] AS [Extent11] ON [Extent2].[CustomerUserFieldId] = [Extent11].[CustomerUserFieldId] LEFT OUTER JOIN [dbo].[CustomerUserField] AS [Extent12] ON [Extent2].[CustomerUserFieldId] = [Extent12].[CustomerUserFieldId] LEFT OUTER JOIN [dbo].[CustomerUserField] AS [Extent13] ON [Extent2].[CustomerUserFieldId] = [Extent13].[CustomerUserFieldId]... </code></pre> <p>This SQL is very slow, as only 1 left outer join is required. Any ideas how I can change my LINQ to only perform a single join?</p> <p>Thanks in advance!</p> <p>Anthony.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload