问题
My code:
string name;
db.Entities.Where(m=>m.name==(name??m.name))
throw:Should be CHAR, but get NCLOB.
//----------------
decimal? id;
db.Entities.Where(m=>m.id==(id??m.id))
The code is working.
my database is oracle.
What should I do to make the code working.?
Thanks.
回答1:
Beware that in Oracle an empty string is NULL.
回答2:
I don't know about Oracle + EF and type mappings. But this is not the way to make predicates optional anyway, because now the part x ?? x.y will always be part of the expression that is translated into SQL. This may hit query optimization and performance.
The way to do this is:
IQueryable<Entity> query = db.Entities;
if (!string.IsNullOrWhiteSpace(name))
{
query = query.Where(m => m.Name == name);
}
来源:https://stackoverflow.com/questions/27200600/comparing-string-and-null-in-linq-to-entity