C
C#7mo ago
TOKYODRIFT!

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
cap5lut7mo 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();
TOKYODRIFT!
TOKYODRIFT!7mo ago
oh
Angius
Angius7mo 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;
}
TOKYODRIFT!
TOKYODRIFT!7mo ago
it is good idea but can we just remove predicate from linq query?
Angius
Angius7mo ago
No, you cannot
cap5lut
cap5lut7mo ago
the predicate needs to be Expression<Func<T, bool>>, i think
TOKYODRIFT!
TOKYODRIFT!7mo ago
ur right
Angius
Angius7mo ago
Yep yep, right
TOKYODRIFT!
TOKYODRIFT!7mo ago
thanks for helping me! I'm sorry but how to use predicate in my case? harold
cap5lut
cap5lut7mo 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
Angius7mo ago
someQuery.WhereIf(x => x.Foo == bar, 2 == 2)
someQuery.WhereIf(x => x.Foo == bar, 2 == 2)
cap5lut
cap5lut7mo 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)
TOKYODRIFT!
TOKYODRIFT!7mo ago
ah I see thanks!