Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
We have a winForms app in which a user car search for Vehicles. My query works fine if a value is given, but if you leave it blank it retrieves nothing ('cos is basically executing .Contains("")).

With a normal SQL query you would use something like this:
"SELECT * FROM Vehicles WHERE Make LIKE '%" + SearchVal + "%'"

If Search val is empty, the query will simply return everything.

But in LINQ I have to do this:
from v in ent.Vehicles
where v.Make.Contains(SearchVal)
Select
{
...
}

Now if Searchval is empty it return nothing (makes sens cos it's looking for nothing). How can I get the desired result here?

Any help would be appreciated.
Thanks
Posted
Comments
Hiren solanki 9-Nov-10 3:31am    
Fire query only when there is searchValue.

1 solution

SQL
from v in ent.Vehicles
where v.Make.Contains(SearchVal) || String.IsNullOrEmpty(SearchVal)
Select
{
...
}
 
Share this answer
 
Comments
NeCroFire 9-Nov-10 8:08am    
So simple, yet it's something I wouldn't have thought of.

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900