C
C#8mo 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
Kiel8mo ago
Expression.AndAlso() might be what you're looking for?
Kiel
Kiel8mo 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
Kiel8mo 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
Saber8mo 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
Maskoe8mo 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
Accord8mo 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
More Posts
❔ Need help on UI Design and tips for implementation - .Net Mauithree questions: 1. How can I edit the blue bar on the top? I'm not sure where its coming from, I o❔ Implementation of Search page in ASP. NET MVC along with stored procedureCan anyone help me to create a webform to search the data from the database to filter it on web page❔ MySQL server issue (Error 28)I'll prefix this with a few things: - I'm not driven with Ubuntu or other unix based systems. - The ✅ data override in dictionary of dictionariesI'm trying to create a dictionary of dictionaries, and use temp variable to create it, but when i chNeed help with homeworkHiya, I've been trying to solve an exercise for a bit but it's boggling me. The exercise is: Write ✅ Databinding using CommunityTooklit.MVVMWhen I am trying to bind to a property on a custom class I am running into some trouble because im n✅ Can anyone give example code of using an Immutable Data Structure for Concurrency?Can anyone give an example that shows how using these types are thread safe?❔ ✅ Databinding using CommunityToolkit.MVVMI am getting intellisense telling me "Member not found in Data context MainViewModel" on lines 23 an❔ Streama audiofile with capability to skip to a specific durationSo for startes, I am making a .net core webapi that uses youtubeAPI to search for music and uses you✅ Process.Start("Something"); Crasheseverytime i launch that it just crash, if u ask why did i put Infernum(); on the first block i just