简体   繁体   中英

SQL Server Compact 4.0 with Entity Framework Conditional String Comparison

It seems that Entity Framework is not able to compare a value against null if that null value is stored in a variable: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1015361-incorrect-handling-of-null-variables-in-where-cl?ref=title

So I'm doing something like this as suggested by other users of SO:

string description = null;
var transactions = from t in entities.Transactions
                   where description == null ? t.Description == null : t.Description == description
                   select t;

While this technique works perfectly fine when dealing with int? or double?, SQL Server Compact throws an EntityCommandExecutionException when using strings (as in the example above). The exception contains the following details:

  InnerException: System.Data.SqlServerCe.SqlCeException
       Message=The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ]
       Source=SQL Server Compact ADO.NET Data Provider

Any idea why I'm getting this and how I can overcome it?

Yeah, apparently ISNULL is funky in Compact .

You could do it on the client, though:

var transactions = description == null ?
                    entities.Transactions.Where(t.Description == null)
                    : entities.Transactions.Where(t.Description == description);

...and I suspect no ISNULL will be generated in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM