Error when using sort in Left Join query

Posts   
 
    
arash
User
Posts: 54
Joined: 16-Dec-2008
# Posted on: 15-Jan-2009 17:46:15   

Hi, I'm using the following builds:

SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll : 2.6.8.1211 SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll : 2.6.9.105

when running the following query against northwind database:

DataAccessAdapter adapter = new DataAccessAdapter();
        LinqMetaData linqMetaData = new LinqMetaData();
        linqMetaData.AdapterToUse = adapter;

var q = from c in linqMetaData.Categories
                join p in linqMetaData.Products
                on c.CategoryId equals p.ProductId into cp
                from product in cp.DefaultIfEmpty()
                orderby product.ProductName descending
                select new { c.CategoryName, product.ProductName };

the generated query is:

SELECT [LPA_L1].[CategoryName], [LPA_L2].[ProductName] FROM ( [Northwind].[dbo].[Categories] [LPA_L1]  LEFT JOIN [Northwind].[dbo].[Products] [LPA_L2]  ON  [LPA_L1].[CategoryID] = [LPA_L2].[ProductID]) ORDER BY [LPLA_4].[ProductName] DESC

the following exception is raised:

The multi-part identifier "LPLA_4.ProductName" could not be bound. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: The multi-part identifier "LPLA_4.ProductName" could not be bound.

Source Error: 


Line 290:               queryToExecute.Command.CommandText = CreateCorrectStoredProcedureName(queryToExecute.Command.CommandText);
Line 291:           }
Line 292:           return base.FetchDataReader(queryToExecute, readerBehavior);
Line 293:       }
Line 294:


Source File: D:\Sample\NortwindSQLTest\DatabaseSpecific\DataAccessAdapter.cs    Line: 292 

Stack Trace: 


[SqlException (0x80131904): The multi-part identifier "LPLA_4.ProductName" could not be bound.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1948826
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4844747
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
   System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
   System.Data.SqlClient.SqlDataReader.get_MetaData() +83
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
   SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior) +86

[ORMQueryExecutionException: An exception was caught during the execution of a retrieval query: The multi-part identifier "LPLA_4.ProductName" could not be bound.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.]
   SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior) +224
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchDataReader(IRetrievalQuery queryToExecute, CommandBehavior readerBehavior) +26
   NorthwindSQLTest.DatabaseSpecific.DataAccessAdapter.FetchDataReader(IRetrievalQuery queryToExecute, CommandBehavior readerBehavior) in D:\Sample\NortwindSQLTest\DatabaseSpecific\DataAccessAdapter.cs:292
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchProjection(List`1 valueProjectors, IGeneralDataProjector projector, IRetrievalQuery queryToExecute, Dictionary`2 typeConvertersToRun) +52
   SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchProjection(List`1 valueProjectors, IGeneralDataProjector projector, IEntityFields2 fields, IRelationPredicateBucket filter, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IGroupByCollection groupByClause, Boolean allowDuplicates, Int32 pageNumber, Int32 pageSize) +245
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.ExecuteValueListProjection(QueryExpression toExecute) +670
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression) +739
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression) +69
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression) +47
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute() +51
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +65
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +7663302
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +61
   _Default.Page_Load(Object sender, EventArgs e) in d:\Sample\WebSite4\Default.aspx.cs:254
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

It happens only with Left join ( DefaultIfEmpty() ) and sorting on a field from right side of the join.

Thank you in advance,

Arash.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39616
Joined: 17-Aug-2003
# Posted on: 15-Jan-2009 21:06:29   

Will look into it.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39616
Joined: 17-Aug-2003
# Posted on: 16-Jan-2009 10:59:02   

Reproduced. Very odd that this happens, the alias should be rewritten with the correct one, and this happens in the projection and also for example when you replace the orderby with where product.Discontinued

which uses a similar pattern as orderby and inside the where predicate, the alias is also correctly rewritten. Looking into it.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39616
Joined: 17-Aug-2003
# Posted on: 16-Jan-2009 11:41:56   

Fixed in next build. I'll attach a new build to this thread.

Frans Bouma | Lead developer LLBLGen Pro
arash
User
Posts: 54
Joined: 16-Dec-2008
# Posted on: 17-Jan-2009 05:21:10   

Otis wrote:

Fixed in next build. I'll attach a new build to this thread.

Confirmed. It is fixed. Thank you.