I took the grids out of the equation and created a couple of unit tests to isolate the issue. I was not able to find a way to get the actual Linq query that was being executed so I crafted my own that created the same "Initial expression to process" that the Telerick MVC grid was creating.
TestMethod1 is equivalent to the actual query being generated and is the one that generates the error below.
The equivalent Linq query works with the Linq2SQL generated metadata but not with LLBLGen.
TestMethod2 (way at the bottom) is what I suspect should actually be generated by the grid.
Note that in both cases, the @LO2a5f6532 parameer is not assigned a value by LLBLGen.
That being said, I don't think that TestMethod1 should fail to generate a valid SQL query.
Thanks for the help!
Jennifer
[TestMethod]
public void TestMethod1()
{
var adapter = new DataAccessAdapter();
var metadata = new LinqMetaData(adapter);
var q = (from item in metadata.Customers
orderby (item != null) ? item.ContactName : null
select item).Take(10);
foreach (var c in q)
Trace.WriteLine(c.ContactName);
}
/*
: Initial expression to process:
value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource2`1[NorthwindDB.EntityClasses.CustomersEntity]).OrderBy(item => IIF((item != null), item.ContactName, null)).Take(10)
M
Generated Sql query:
Query: SELECT TOP(@top0) [LPLA_1].[CustomerID] AS [CustomerId], [LPLA_1].[CompanyName], [LPLA_1].[ContactName], [LPLA_1].[ContactTitle], [LPLA_1].[Address], [LPLA_1].[City], [LPLA_1].[Region], [LPLA_1].[PostalCode], [LPLA_1].[Country], [LPLA_1].[Phone], [LPLA_1].[Fax] FROM [Northwind].[dbo].[Customers] [LPLA_1] ORDER BY CASE WHEN CASE WHEN () THEN 1 ELSE 0 END=1 THEN [LPLA_1].[ContactName] ELSE @LO1d4fb92 END ASC
Parameter: @top0 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 10.
Parameter: @LO1d4fb92 : AnsiString. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.
*/
Test method NorthwindMVCWeb.Tests.NWLinqTest.TestMethod5 threw exception: SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of a retrieval query: Incorrect syntax near ')'.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ---> System.Data.SqlClient.SqlException: Incorrect syntax near ')'..
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior)
--- End of inner exception stack trace ---
at SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery.Execute(CommandBehavior behavior)
at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteMultiRowRetrievalQuery(IRetrievalQuery queryToExecute, IEntityFactory2 entityFactory, IEntityCollection2 collectionToFill, IFieldPersistenceInfo[] fieldsPersistenceInfo, Boolean allowDuplicates, IEntityFields2 fieldsUsedForQuery)
at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollectionInternal(IEntityCollection2 collectionToFill, ref IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, ExcludeIncludeFieldsList excludedIncludedFields, Int32 pageNumber, Int32 pageSize)
at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchEntityCollection(IEntityCollection2 collectionToFill, IRelationPredicateBucket filterBucket, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList excludedIncludedFields, Int32 pageNumber, Int32 pageSize)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.ExecuteEntityProjection(QueryExpression toExecute) in LLBLGenProProvider2.cs: line 140
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression) in LLBLGenProProviderBase.cs: line 258
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression) in LLBLGenProProviderBase.cs: line 93
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression) in LLBLGenProProviderBase.cs: line 709
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute() in LLBLGenProQuery.cs: line 87
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() in LLBLGenProQuery.cs: line 162
at NorthwindMVCWeb.Tests.NWLinqTest.TestMethod5() in NWLinqTest.cs: line 118
[TestMethod]
public void TestMethod2()
{
var adapter = new DataAccessAdapter();
var metadata = new LinqMetaData(adapter);
var q = (from item in metadata.Customers
orderby (item.ContactName != null) ? item.ContactName : null
select item).Take(10);
foreach (var c in q)
Trace.WriteLine(c.ContactName);
}
/*
: Initial expression to process:
value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource2`1[NorthwindDB.EntityClasses.CustomersEntity]).OrderBy(item => IIF((item.ContactName != null), item.ContactName, null)).Take(10)
Generated Sql query:
Query: SELECT TOP(@top0) [LPLA_1].[CustomerID] AS [CustomerId], [LPLA_1].[CompanyName], [LPLA_1].[ContactName], [LPLA_1].[ContactTitle], [LPLA_1].[Address], [LPLA_1].[City], [LPLA_1].[Region], [LPLA_1].[PostalCode], [LPLA_1].[Country], [LPLA_1].[Phone], [LPLA_1].[Fax] FROM [Northwind].[dbo].[Customers] [LPLA_1] ORDER BY CASE WHEN CASE WHEN ( [LPLA_1].[ContactName] IS NOT NULL) THEN 1 ELSE 0 END=1 THEN [LPLA_1].[ContactName] ELSE @LO2a5f6532 END ASC
Parameter: @top0 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 10.
Parameter: @LO2a5f6532 : AnsiString. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.
*/