Hi I've also used expression trees for dynamic filtering, searching etc. I've implemented a query builder class which can apply fitering, searching, sorting and returns a queryable to be used as EF core predicate. My question is, for example if we have many filter paramaters there will be lots of if conditions to check whether the value is null. How can we simplify this process using query builder class to rid of if checks. I wonder your comment.
Why would one go this complex route instead of using IQueryable? var query = dbContext.Associations.AsQueryable(); if (!string.IsNullOrEmpty(request.Name)) { query = query.Where(a => a.Name.Contains(request.Name)); } return await query.ToListAsync(cancellationToken: cancellationToken);
Hey Robert, Thanks for your comment! You’re absolutely right that using IQueryable with simple filtering like Contains works well in many cases. However, the power of expression trees comes into play when dealing with more complex scenarios. In your example, you're handling a specific condition (Contains) with a direct if..else approach, which is perfectly fine for simpler filters. But when you start dealing with a more extensive set of dynamic filters, such as combinations of comparison operators (>=,
Useful information, thanks for sharing!
Glad you enjoyed it!
This content is amazing, thanks for sharing
Thanks for watching!
Looks Complex but understandable. Thanks ❤
Hey, thanks a lot. Enjoy watching
Hi I've also used expression trees for dynamic filtering, searching etc. I've implemented a query builder class which can apply fitering, searching, sorting and returns a queryable to be used as EF core predicate.
My question is, for example if we have many filter paramaters there will be lots of if conditions to check whether the value is null. How can we simplify this process using query builder class to rid of if checks. I wonder your comment.
Hey, of course. We have a visitor pattern to get rid of 'ıf' checks everytime. Check of ExpressionVisitor class
Why would one go this complex route instead of using IQueryable?
var query = dbContext.Associations.AsQueryable();
if (!string.IsNullOrEmpty(request.Name))
{
query = query.Where(a => a.Name.Contains(request.Name));
}
return await query.ToListAsync(cancellationToken: cancellationToken);
9:55
@@MAUIMS-m4g The IFs in this case are readable and easy to maintain. There's no else.
Hey Robert,
Thanks for your comment! You’re absolutely right that using IQueryable with simple filtering like Contains works well in many cases. However, the power of expression trees comes into play when dealing with more complex scenarios.
In your example, you're handling a specific condition (Contains) with a direct if..else approach, which is perfectly fine for simpler filters. But when you start dealing with a more extensive set of dynamic filters, such as combinations of comparison operators (>=,
Are you Indian or not. I can't stand them to even listen to.
Am I sounding like Indian?