I am writing a user interface that allows someone to search for users by their first and / or last name. For example, if you typed the name “Mike” for the first name and “Joe” for the last name, it will return “Mike Jones,” “Mike Johnson,” and “Mike Jobs.” For this search, I use the following LINQ statement:
var users = (from u in context.TPM_USER where u.LASTNAME.ToLower().Contains(LastName.ToLower()) && u.FIRSTNAME.ToLower().Contains(FirstName.ToLower()) select u);
(Maybe or maybe not the best way to do a case-insensitive example, but it seems to work)
The problem is that the user enters a first or last name, but then leaves the other field blank. If I type “Mike” for the first name and leave the Last Name field blank, I want to return all the Mikes regardless of their last name. The above query does not return any results if both fields are not filled at least with something.
I tried:
var users = (from u in context.TPM_USER where (LastName == "" || u.LASTNAME.ToLower().Contains(LastName.ToLower())) && (FirstName == "" || u.FIRSTNAME.ToLower().Contains(FirstName.ToLower())) select u);
However, I still do not get any results if both fields are blank. I checked under the debugger that LastName == ""
really true.
UPDATE:
I did a few more debugs, and this is actually an Oracle problem. Generated request:
--Replaced the field list with * for brevity SELECT * FROM TPMDBO.TPM_USER "Extent1" WHERE (('jones' = '') OR ((INSTR(LOWER("Extent1".LASTNAME), LOWER('jones'))) > 0)) AND (('' = '') OR ((INSTR(LOWER("Extent1".FIRSTNAME), LOWER(''))) > 0))
At first glance it seems correct. However, Oracle does not seem to short the phrase ('' = '')
correctly. In fact, if I do this:
select * from TPM_USER where '' = ''
I get null strings. I do not have enough Oracle expert to know how this request should be written, but in any case this is an error of the Entity Framework dialect.