I have a LINQ query:
Dim result = DataContext.Items.Select(Function(p) New With {
.Category = If(p.Category IsNot Nothing, p.Category.Name, String.Empty)
.Name = p.Name
})
This works fine against a SQL Server 2005,2008,2012 DB. When I run it against a SQL CE 4.0 DB, I get this exception:
Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument value for the function is not valid. [ Argument # = 3, Name of function(if known) = case ]
I've isolated the problem to String.Empty
. If I use ""
directly, the code runs fine. It also works if I use .Category = String.Empty
. Seems like a bug in how SqlCe deals with a MemberAccessExpression
inside an If
MethodCallExpression
.
If I take out the .Category
line, everything works fine. What's the right way to do a null check in LINQ to SQL CE 4? I've checked Skeet's answer, but I actually want to check the DB value, not my value. I've also seen this answer, but it would load everything into memory which would defeat the purpose of using LINQ.
I still have the exception after upgrading to the EF 6 RC (as per ErikEJ's suggestion). Here's a stack trace:
at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.SqlServerCompact.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.SqlServerCompact.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)