How to check if a predicate expression has been changed?

 var predicate = PredicateBuilder.True<o_order>();

This is my predicate expression, where in some specific conditions I will add expressions with it.

Similarly

 if (!string.IsNullOrEmpty(param.sSearch))
                predicate = predicate.And(s => s.OrderID.ToString().Contains(param.sSearch));

Now my question is: if this expression does not satisfy this condition, then will there be any expression? and how would I know if this returns an expression with him.

Just want to do -

if(predicate==null) or if(predicate contains no expression)

+4
source share
3 answers

, , , . PredicateBuilder ( , predicate = predicate.And..., pred), .

var predicate = PredicateBuilder.True<o_order>();
var oldPredicate = predicate;

if (!string.IsNullOrEmpty(param.sSearch))
    predicate = predicate.And(s => ........... );  // replace!

if (!string.IsNullOrEmpty(....))
    predicate = predicate.And(s => ........... );  // replace!

if(predicate == oldPredicate)   // was it changed?
    ; // no filters applied
else
    ; // some filters applied

, . , ( , ):

var predicate = PredicateBuilder.True<o_order>();
var oldPredicate = predicate;

bool case1applied = !string.IsNullOrEmpty(....);
if (case1applied)
    predicate = predicate.And(s => ........... );

bool case2applied = !string.IsNullOrEmpty(....);
if (case2applied)
    predicate = predicate.And(s => ........... );

if(predicate == oldPredicate) // or the hard way: !case1applied && !case2applied
    ; // no filters applied
else
    if(case1applied && case2applied) // all filters applied
    else ....
+3

, , PredicateBuilder , . , PredicateBuilder

var predicate = PredicateBuilder.True<o_order>();
var cond1 = /* condition 1 */
var cond2 = /* condition 2 */
...
if (cond1) { /* add expression for condition 1 */ }
if (cond2) { /* add expression for condition 2 */ }
...
if (!cond1 && !cond2 && ...) { /* handle case of no expressions added */ }
+2

, IsStarted , .

In my code, I used this to create composite search operators, where search parameters are optional. For instance.

var quoteDatePredicate= PredicateBuilder.New<SearchData>();

if (searchCriteria.QuoteFromDate.HasValue)
{
    quoteDatePredicate.And(x => x.QuoteDate >= searchCriteria.QuoteFromDate);
}

var saleDatePredicate = PredicateBuilder.New<SearchData>();

if (searchCriteria.SaleDate.HasValue)
{
    saleDatePredicate.And(x => x.SaleDate >= searchCriteria.SaleDateFrom);
}  

And then I create another predicate variable and use the operator Ifto add any predicates that were actually assigned:

var datesPredicate = PredicateBuilder.New<SearchData>();

if (quoteDatePredicate.IsStarted) datesPredicate.Or(quoteDatePredicate);
if (saleDatePredicate.IsStarted) datesPredicate.Or(saleDatePredicate);

So far, this seems to work fine in my code.

Alternatively, comparing the assigned and unassigned predicate variable in the debugger seems to suggest that you can use this to check if the predicate has been assigned:

if (dueOutOfDatePredicate.Parameters[0].Name = "f")

I have not even tried this.

+2
source

All Articles