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.