I have a method that receives a category id, followed by two optional string parameters that default to null.
I tried using a few similar answers from other questions on SO but sofar none have helped.
I am trying to get the linq to EF query to work as follows:
If either optional parameter has a value, use the value otherwise use an Is Null.
If both optional parameters are present use these as part of the query or either one if only on eis supplied. But if no parmeters are added, just use the category id.
Both optional parameters in the db are marked as nullable.
Here is the code that's not working:
from c in dtx.Categories
where c.CategoryId == CatId
&& (string.IsNullOrEmpty(param1) ? c.Param1 == null : c.Param1 == param1)
&& (string.IsNullOrEmpty(param2) ? c.Param2 == null : c.Param2 == Param2)
select c
Try Two:
from c in dtx.Categories
where c.CategoryId == CatId
&& (c.Param1 == null ? c.Param1 == null : c.Param1 == param1)
&& (c.Param2 == null ? c.Param2 == null : c.Param2 == param2)
select c
No Errors are thrown, but both queries always return zero results unless both parameters are there.
One of the posts I tried: How can i query for null values in entity framework?