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
精彩评论