comparing “string” and “null” in linq to entity

落爺英雄遲暮 提交于 2019-12-11 11:59:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!