开发者

entity framework and where with like [duplicate]

开发者 https://www.devze.com 2023-02-09 23:08 出处:网络
This question already has answers here: Like Operator in Entity Framework? (9 answers) Closed 3 years ago.开发者_C百科
This question already has answers here: Like Operator in Entity Framework? (9 answers) Closed 3 years ago.开发者_C百科

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


You can use Contains():

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消