entity framework and where with like [duplicate]

假如想象 提交于 2019-12-10 10:13:31

问题


I' m using this instruction:

db_user = db.CBR_User.FirstOrDefault(p => p.Codice_Fiscale == code);

I want use "like" operator instead == for manage case insensitive

How can do it?

thanks


回答1:


If you want to do an equality comparison anyway, I suggest two ways:

db_user = db.CBR_User.FirstOrDefault(p => p.Codice_Fiscale.Equals(code, StringComparison.OrdinalIgnoreCase));

Or

db_user = db.CBR_User.FirstOrDefault(p => p.Codice_Fiscale.ToUpper() == code.ToUpper());

If you don't want that, you can use StartsWith, Contains, etc, with the parameter StringComparison.OrdinalIgnoreCase.




回答2:


You can use Contains():

db_user = db.CBR_User.FirstOrDefault(p => p.Codice_Fiscale.Contains(code));



回答3:


I know this is an old topic, but to expand on what Kristof Claes was saying,

db_user = db.CBR_User.FirstOrDefault(p => p.Codice_Fiscale.ToUpper().Contains(code.ToUpper()));

if you use the ToUpper option, it will act as if it is a case insensitive search. You could use ToLower to do the same thing, in case you have that penchant.




回答4:


In LINQ To Entity you have functions like StartsWith, EndsWith and Contains that you can use instead like



来源:https://stackoverflow.com/questions/4958082/entity-framework-and-where-with-like

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