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.
@TuralSuleymaniTech
23 күн бұрын
Hey, of course. We have a visitor pattern to get rid of 'ıf' checks everytime. Check of ExpressionVisitor class
@robertmrobo8954
25 күн бұрын
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);
@MAUIMS-m4g
24 күн бұрын
9:55
@robertmrobo8954
24 күн бұрын
@@MAUIMS-m4g The IFs in this case are readable and easy to maintain. There's no else.
@TuralSuleymaniTech
24 күн бұрын
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 (>=,
@dr.angerous
22 күн бұрын
Are you Indian or not. I can't stand them to even listen to.
Пікірлер: 14