Pierre-Alain Vigeant
Pierre-Alain Vigeant

Reputation: 23093

How to write linq query to prevent duplicates joins?

I have a query that search for all accommodations in an order, sorted by day. When I check on the sever what query is executed, I see multiple join toward the same table on the same keys

var parcourt = this.DataService.From<OrderItem>()
                               .Where(i => i.OrderId == orderId && i.Product.ProductTypeId == (int)ProductTypes.Accommodation)
                               .OrderBy(i => i.DayNumber)
                               .ThenBy(i => i.OrderItemId)
                               .Select(i => new
                               {
                                   i.OrderItemId,
                                   i.DayNumber,
                                   i.Product.Establishment.Address,
                                   i.Product.Establishment.Coordinates
                               });

If you check the resulting SQL (as show by ToTraceString), you can see two join on the Products and Establishments table.

SELECT 
[Project1].[OrderItemId] AS [OrderItemId], 
[Project1].[DayNumber] AS [DayNumber], 
[Project1].[Address] AS [Address], 
[Project1].[EstablishmentId] AS [EstablishmentId], 
[Project1].[Latitude] AS [Latitude], 
[Project1].[Longitude] AS [Longitude]
FROM ( SELECT 
    [Extent1].[OrderItemId] AS [OrderItemId], 
    [Extent1].[DayNumber] AS [DayNumber], 
    [Extent4].[Address] AS [Address], 
    [Extent5].[EstablishmentId] AS [EstablishmentId], 
    [Extent5].[Latitude] AS [Latitude], 
    [Extent5].[Longitude] AS [Longitude]
    FROM     [dbo].[OrderItems] AS [Extent1]
    INNER JOIN [dbo].[Products] AS [Extent2] ON [Extent1].[ProductId] = [Extent2].[ProductId]
    LEFT OUTER JOIN [dbo].[Products] AS [Extent3] ON [Extent1].[ProductId] = [Extent3].[ProductId]
    LEFT OUTER JOIN [dbo].[Establishments] AS [Extent4] ON [Extent3].[EstablishmentId] = [Extent4].[EstablishmentId]
    LEFT OUTER JOIN [dbo].[Establishments] AS [Extent5] ON [Extent3].[EstablishmentId] = [Extent5].[EstablishmentId]
    WHERE (1 = [Extent2].[ProductTypeId]) AND ([Extent1].[OrderId] = @p__linq__0)
)  AS [Project1]
ORDER BY [Project1].[DayNumber] ASC, [Project1].[OrderItemId] ASC

How can I prevent this linq-to-entities from joining twice on a table? How can I rewrite the query to avoid this situation?

The table structure goes as follow (simplified):

Table schema

This is the query

Upvotes: 4

Views: 1495

Answers (1)

Gabriel GM
Gabriel GM

Reputation: 6639

Could you try this query? I think if you call all your joins explicitly, it'll not create joins automatically.

var parcourt = (from i in this.DataService.OrderItem
                join p in this.DataService.Product on p.ProductId equals i.ProductId
                join e in this.DataService.Establishments on e.EstablishmentId equals p.EstablishmentId
                where i.OrderId == orderId && p.ProductTypeId == (int)ProductTypes.Accomodation
                orderby i.DayNumber, i.OrderItemId
                select new
                {
                    i.OrderItemId,
                    i.DayNumber,
                    e.Address,
                    e.Coordinates
                });

Upvotes: 3

Related Questions