Not selecting correct column names on derived tables

Posts   
 
    
joenathan
User
Posts: 27
Joined: 28-Jun-2011
# Posted on: 09-Dec-2011 18:19:29   

I'm using the Northwind database and the self-servicing code.

When I use this this code to generate a query, it works perfectly.


            LinqMetaData metaData = new LinqMetaData();

            IQueryable<int> orderIds = metaData.Order.Where(o => o.ShipCountry == "USA").Select(o => o.OrderId);


            orderIds = (from orders in orderIds
                        join filter in metaData.OrdersQry on orders equals filter.OrderId
                        select orders);

            var result = (from orders in orderIds 
                          join details in metaData.OrderDetailsExtended on orders equals details.OrderId
                        select details).ToList();

it generates this SQL


exec sp_executesql N'
SELECT DISTINCT 
[LPA_L2].[Discount], 
[LPA_L2].[ExtendedPrice], 
[LPA_L2].[OrderID] AS [OrderId], 
[LPA_L2].[ProductID] AS [ProductId],
[LPA_L2].[ProductName],
[LPA_L2].[Quantity], 
[LPA_L2].[UnitPrice] 
FROM ( (
   SELECT [LPA_L3].[OrderId] 
   FROM ( (
      SELECT [LPLA_1].[OrderID] AS [OrderId] 
      FROM [Northwind].[dbo].[Orders]  [LPLA_1]   
      WHERE ( ( ( [LPLA_1].[ShipCountry] = @p1)))) [LPA_L3]  
   INNER JOIN [Northwind].[dbo].[Orders Qry] [LPA_L4] 
      ON  [LPA_L3].[OrderId] = [LPA_L4].[OrderID])) [LPA_L1]  
INNER JOIN [Northwind].[dbo].[Order Details Extended] [LPA_L2]  
      ON  [LPA_L1].[OrderId] = [LPA_L2].[OrderID])',N'@p1 nvarchar(15)'
,@p1=N'USA'

However if I change add one line


            LinqMetaData metaData = new LinqMetaData();

            IQueryable<int> orderIds = metaData.Order.Where(o => o.ShipCountry == "USA").Select(o => o.OrderId);


            orderIds = (from orders in orderIds
                        join filter in metaData.OrdersQry on orders equals filter.OrderId
[b]                     where filter.PostalCode == "83720"[/b]
                        select orders);

            var result = (from orders in orderIds 
                          join details in metaData.OrderDetailsExtended on orders equals details.OrderId
                        select details).ToList();

then it doesn't generate the correct sql and errors out


exec sp_executesql N'
SELECT DISTINCT 
[LPA_L2].[Discount], 
[LPA_L2].[ExtendedPrice], 
[LPA_L2].[OrderID] AS [OrderId], 
[LPA_L2].[ProductID] AS [ProductId], 
[LPA_L2].[ProductName], 
[LPA_L2].[Quantity], 
[LPA_L2].[UnitPrice] 
FROM ( (
[b]   SELECT [LPA_L3].[orders] FROM ( ([/b]
      SELECT [LPLA_1].[OrderID] AS [OrderId] 
      FROM [Northwind].[dbo].[Orders]  [LPLA_1]   
      WHERE ( ( ( [LPLA_1].[ShipCountry] = @p1)))) [LPA_L3]  
   INNER JOIN [Northwind].[dbo].[Orders Qry] [LPA_L4]  
      ON  [LPA_L3].[OrderId] = [LPA_L4].[OrderID]) 
   WHERE ( ( ( [LPA_L4].[PostalCode] = @p2)))) [LPA_L1]  
INNER JOIN [Northwind].[dbo].[Order Details Extended] [LPA_L2]  
   ON  [LPA_L1].[orders] = [LPA_L2].[OrderID])',N'@p1 nvarchar(15),@p2 nvarchar(10)',@p1=N'USA',@p2=N'83720'

As you can see it tries to select orders from LPA_L3, which doesn't exist. If I change the "orders" variable in the following linq statement, to <variableName>, then it will subsequently generate sql that will try to select from LPA_L3.dbo.<variableName>


            orderIds = (from orders in orderIds
                        join filter in metaData.OrdersQry on orders equals filter.OrderId
                    where filter.PostalCode == "83720"
                        select orders);

Also a corresponding question, why is the distinct being thrown into the generated SQL. That doesn't seem correct since there's no distinct/group by or anything else in the LINQ that would limit down duplicates.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Dec-2011 22:15:30   
David Elizondo | LLBLGen Support Team
joenathan
User
Posts: 27
Joined: 28-Jun-2011
# Posted on: 21-Dec-2011 15:41:18   

The runtime version is v2.0.50727 LLBLGen version 3.1.0.0

Walaa avatar
Walaa
Support Team
Posts: 14951
Joined: 21-Aug-2005
# Posted on: 21-Dec-2011 15:48:20   

That's not the correct RTL version (build no.)

When it's a problem occuring at runtime, post the Runtime library version. The runtime library version is obtainable by rightclicking the SD.LLBLGen.Pro.ORMSupportClasses.NETxy.dll in windows explorer and then by selecting properties and the version tab. The version is then enlisted at the top as the fileversion. It has the typical format as 2.0.0.YYMMDD, or starting in 2007, the format v.0.YY.MMDD, where 'v' is the version number, e.g. '3'.

Similarly with the LinqSupprt... file.

joenathan
User
Posts: 27
Joined: 28-Jun-2011
# Posted on: 22-Dec-2011 16:52:58   

3.1.11.207 for both

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39619
Joined: 17-Aug-2003
# Posted on: 22-Dec-2011 22:30:37   

please download the latest build and try again.

Frans Bouma | Lead developer LLBLGen Pro