I am having an issue with getting a Multiple search working with a IQueryable.Where Clause using the AND operator. I know that the problem is when a parameter is blank or null it is searching for a “” with the other parameter and not returning results but I don’t want to have to go through an extended and ridiculously nested if to check for nulls or blanks on 8 parameters. I have googled this for days, the OR works obviously but the AND condition returns nothing.
albums = albums.Where(a => a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper()) &&
a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper()));
The above works if both parameters are not blank but returns nothing if one of them is. I have tried building the where based on not null like below but same results:
if (!String.IsNullOrEmpty(searchName))
{
albums = albums.Where(a => a.CA_AlbumName.ToUpper().Contains(searchName.ToUpper()));
}
if (!String.IsNullOrEmpty(searchURL))
{
albums = albums.Where(a => a.CA_AlbumURL.ToUpper().Contains(searchURL.ToUpper()));
}
If I understand your requirement right, the following one should work:
@James is right. The updated code is here: