I have a LINQ expression that joins two tables. I want to conditionally check another boolean value: (notice text between *********
below)
bool status = testfunc();
var List =
from t in Houses
join k in Tbl_HouseOwner on t.HouseCode equals k.HouseCode
where k.ReqCode== t.ReqCode
*********** if (status) { where k.Name.Contains(Name) } **********
select new
{
...
name = k.Name,
...
};
You can use status
to mask the condition, like this:
where k.ReqCode == t.ReqCode && (!status || k.Name.Contains(Name))
If the status
is false
, the OR ||
will succeed immediately, and the AND &&
will be true (assuming that we've got to evaluating the OR ||
, the left-hand side of the AND &&
must have been true). If the status
is true
, on the other hand, the k.Name.Contains(Name)
would need to be evaluated in order to finish evaluating the condition.