C
C#14mo ago
Maskoe

❔ 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 Ok 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
public static IQueryable<T> OptionalWhere<T, T2>(this IQueryable<T> source, Expression<Func<T, T2>> selector, T2 value)
{
if (value == null)
return source;

return source.Where(x => selector.Compile()(x).Equals(value));
}
public static IQueryable<T> OptionalWhere<T, T2>(this IQueryable<T> source, Expression<Func<T, T2>> selector, T2 value)
{
if (value == null)
return source;

return source.Where(x => selector.Compile()(x).Equals(value));
}
Func cant be translated. Expression cant be translated if I compile it. I guess because it turns into a func..
public static IQueryable<TSource> WhereNotNull<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
{
// somehow look into the predicate whether its an equals clause and check if the "right side" is null and return source back
return source.Where(predicate);
}
public static IQueryable<TSource> WhereNotNull<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
{
// somehow look into the predicate whether its an equals clause and check if the "right side" is null and return source back
return source.Where(predicate);
}
6 Replies
Kiel
Kiel14mo ago
Expression.AndAlso() might be what you're looking for?
Kiel
Kiel14mo ago
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...
Kiel
Kiel14mo ago
i think what you want to do is combine two expressions, one that's "hardcoded" (x != null) and the one passed in (selector)
Saber
Saber14mo ago
I did something like this
public static IQueryable<T> WhereIf<T>(this IQueryable<T> queryable, bool condition, Expression<Func<T, bool>> func)
=> condition ? queryable.Where(func) : queryable;

public static IQueryable<T> WhereIfNotNull<T, U>(this IQueryable<T> queryable, U item, Expression<Func<T, bool>> func)
=> item != null ? queryable.Where(func) : queryable;

public static IQueryable<T> WhereIfNotEmpty<T>(this IQueryable<T> queryable, string item, Expression<Func<T, bool>> func)
=> !string.IsNullOrEmpty(item) ? queryable.Where(func) : queryable;
public static IQueryable<T> WhereIf<T>(this IQueryable<T> queryable, bool condition, Expression<Func<T, bool>> func)
=> condition ? queryable.Where(func) : queryable;

public static IQueryable<T> WhereIfNotNull<T, U>(this IQueryable<T> queryable, U item, Expression<Func<T, bool>> func)
=> item != null ? queryable.Where(func) : queryable;

public static IQueryable<T> WhereIfNotEmpty<T>(this IQueryable<T> queryable, string item, Expression<Func<T, bool>> func)
=> !string.IsNullOrEmpty(item) ? queryable.Where(func) : queryable;
Maskoe
MaskoeOP14mo ago
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)
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server