C
C#13mo ago
SWEETPONY

Is it possible to remove predicate by condition?

I have following:
var notificationModels = await _repository
.Query()
.Where(model => DateTimeOffset.Now <= model.ExpiresAt)
.Where(model => model.To
.Contains(operatorName))
.Where(model => model.Id <= arguments.LastNotificationId)
.OrderByDescending(model => model.Id)
.Limit(arguments.TotalCount)
.ToList();
var notificationModels = await _repository
.Query()
.Where(model => DateTimeOffset.Now <= model.ExpiresAt)
.Where(model => model.To
.Contains(operatorName))
.Where(model => model.Id <= arguments.LastNotificationId)
.OrderByDescending(model => model.Id)
.Limit(arguments.TotalCount)
.ToList();
LastNotificationId can be null or 0 and this case I wanna remove .Where(model => model.Id <= arguments.LastNotificationId) and return just limited number of items
13 Replies
cap5lut
cap5lut13mo ago
u can split the method chaining up:
var query = _repository
.Query()
.Where(model => DateTimeOffset.Now <= model.ExpiresAt)
.Where(model => model.To
.Contains(operatorName));

if (check last modification id here)
query = query.Where(model => model.Id <= arguments.LastNotificationId);

var notificationModels = await query
.OrderByDescending(model => model.Id)
.Limit(arguments.TotalCount)
.ToList();
var query = _repository
.Query()
.Where(model => DateTimeOffset.Now <= model.ExpiresAt)
.Where(model => model.To
.Contains(operatorName));

if (check last modification id here)
query = query.Where(model => model.Id <= arguments.LastNotificationId);

var notificationModels = await query
.OrderByDescending(model => model.Id)
.Limit(arguments.TotalCount)
.ToList();
SWEETPONY
SWEETPONYOP13mo ago
oh
Angius
Angius13mo ago
You can also make a nice extension method like
public IQueryable<T> WhereIf<T>(this IQueryable<T> query, Func<T, bool> predicate, bool condition)
{
return condition
? query.Where(predicate)
: query;
}
public IQueryable<T> WhereIf<T>(this IQueryable<T> query, Func<T, bool> predicate, bool condition)
{
return condition
? query.Where(predicate)
: query;
}
SWEETPONY
SWEETPONYOP13mo ago
it is good idea but can we just remove predicate from linq query?
Angius
Angius13mo ago
No, you cannot
cap5lut
cap5lut13mo ago
the predicate needs to be Expression<Func<T, bool>>, i think
SWEETPONY
SWEETPONYOP13mo ago
ur right
Angius
Angius13mo ago
Yep yep, right
SWEETPONY
SWEETPONYOP13mo ago
thanks for helping me! I'm sorry but how to use predicate in my case? harold
cap5lut
cap5lut13mo ago
the predicate is the same as in the normal Where call, just afterwards u pass a boolean value that if true actually adds the check
Angius
Angius13mo ago
someQuery.WhereIf(x => x.Foo == bar, 2 == 2)
someQuery.WhereIf(x => x.Foo == bar, 2 == 2)
cap5lut
cap5lut13mo ago
never tried it with IQueryable and Expression but i guess theoretically u could do something like
.Where(condition ? model => model.Id <= arguments.LastNotificationId : model => true)
.Where(condition ? model => model.Id <= arguments.LastNotificationId : model => true)
so it would simply add your perdicate or an "always true" predicate. but in normal linq its a horrible idea because this will check the "always true" predicate on each element as well (even if not needed) and i guess the generated sql would look quite weird as well (and maybe the dbms would do the check for every row as well)
SWEETPONY
SWEETPONYOP13mo ago
ah I see thanks!
Want results from more Discord servers?
Add your server