Aliasing bug when using joins and ordering by multiple fields

Posts   
 
    
Jez
User
Posts: 198
Joined: 01-May-2006
# Posted on: 17-Mar-2008 19:52:00   

Hi Frans

I have the following query against the Northwind database:

LinqMetaData meta = new LinqMetaData(adapter);

var query = from e in meta.Employee
        join o in meta.Order on e.Id equals o.EmployeeId
        join c in meta.Customer on o.CustomerId equals c.CustomerId
        where c.CustomerId == "ALFKI"
        select e;

query = query.OrderBy(e => e.City).ThenBy(e => e.Region);

Note that I renamed the EmployeeId field to just Id

This causes the following exception: "Invalid column name 'EmployeeID'."

Because the following invalid SQL was generated:

exec sp_executesql N'
SELECT 
    [LPA_L1].[EmployeeID] AS [Id], 
    [LPA_L1].[LastName], [LPA_L1].[FirstName], 
    [LPA_L1].[Title], [LPA_L1].[TitleOfCourtesy], 
    [LPA_L1].[BirthDate], 
    [LPA_L1].[HireDate], 
    [LPA_L1].[Address], 
    [LPA_L1].[City], 
    [LPA_L1].[Region], 
    [LPA_L1].[PostalCode], 
    [LPA_L1].[Country], 
    [LPA_L1].[HomePhone], 
    [LPA_L1].[Extension], 
    [LPA_L1].[Photo], 
    [LPA_L1].[Notes], 
    [LPA_L1].[ReportsTo], 
    [LPA_L1].[PhotoPath] 
FROM (
    SELECT 
        [LPA_L2].[EmployeeID] AS [Id], 
        [LPA_L2].[LastName], 
        [LPA_L2].[FirstName], 
        [LPA_L2].[Title], 
        [LPA_L2].[TitleOfCourtesy], 
        [LPA_L2].[BirthDate], 
        [LPA_L2].[HireDate], 
        [LPA_L2].[Address], 
        [LPA_L2].[City], 
        [LPA_L2].[Region], 
        [LPA_L2].[PostalCode], 
        [LPA_L2].[Country], 
        [LPA_L2].[HomePhone], 
        [LPA_L2].[Extension], 
        [LPA_L2].[Photo], 
        [LPA_L2].[Notes], 
        [LPA_L2].[ReportsTo], 
        [LPA_L2].[PhotoPath] 
    FROM (( 
        [Northwind].[dbo].[Employees] [LPA_L2]  
        INNER JOIN [Northwind].[dbo].[Orders] [LPA_L3]  
            ON  [LPA_L2].[EmployeeID] = [LPA_L3].[EmployeeID]) 
        INNER JOIN [Northwind].[dbo].[Customers] [LPA_L4]  
            ON  [LPA_L3].[CustomerID] = [LPA_L4].[CustomerID]) 
        WHERE ( ( ( [LPA_L4].[CustomerID] = @CustomerId1)))) LPA_L1 
    ORDER BY 
        [LPA_L1].[City] ASC,
        [LPA_L1].[Region] ASC

',N'@CustomerId1 nchar(5)',@CustomerId1=N'ALFKI'

The outer query is trying to select EmployeeID as ID, but the inner query has already aliased it to ID.

If I remove the order by clause, the query works correctly as no subquery is created.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-Mar-2008 20:25:11   

This is with today's build? (or with the original build) ? Never mind, the alias bug fixed in todays build isn't about this situation.

Indeed, something which should work. Will look into it.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Mar-2008 19:03:50   

Reproduced.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-Mar-2008 11:52:53   

Heh simple_smile I was looking at where to fix it, so I first added some code to the linq provider, then that failed and it wasn't a good idea, so I looked for a way to add a routine to runtime lib, where I found a routine which was already added for correcting the situation of your query (sometimes frameworks just get too big to get an overview of all the methods available wink ), but it had a small bug, after correcting that your query worked.

So fixed in next build.

Frans Bouma | Lead developer LLBLGen Pro
Jez
User
Posts: 198
Joined: 01-May-2006
# Posted on: 19-Mar-2008 12:14:00   

Brilliant, thanks simple_smile