❔ EF Core optional where extension method
I have a lot of optional parameters for queries. Filter by this, filter by that etc.
I can do it like this
context.table.Where(x => projectId == null || x.ProjectId == projectId)
(This actually doesnt even add a WHERE TRUE to the generated SQL Query would like to avoid the null check or part every time and write something like .OptionalWhere(x => x.ProjectId, projectId)
but I cant find the correct syntax for the extension method.
Im trying stuff similiar to this
Func cant be translated. Expression cant be translated if I compile it. I guess because it turns into a func..
6 Replies
Expression.AndAlso()
might be what you're looking for?Stack Overflow
Combining two expressions (Expression>)
I have two expressions of type Expression<Func<T, bool>> and I want to take to OR, AND or NOT of these and get a new expression of the same type
Expression<Func<T, bool>> e...
i think what you want to do is combine two expressions, one that's "hardcoded" (
x != null
) and the one passed in (selector)I did something like this
yea but these barely gain anything in being more concise, if its now 2 arguments in the method, instead of 2 conditions in the where clause
.WhereIfNotNull(myId, x => x.Id == myId)
vs
.Where(x => myId == null || x.Id == myId)
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.