Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if predicate expression was changed?

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

This is my predicate expression, where on some certain conditions I will append expressions with it.

Likewise

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

Now my question is if this expression doesn't pass by this condition then would there be any expression? and how would I know if it returns no expression with it.

Simply I want to do-

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

like image 940
Manoz Avatar asked Jun 25 '15 09:06

Manoz


1 Answers

I've found that the IsStarted property seems to show if a predicate has been assigned.

In my code I've used that to build up composite search statements where the search parameters are optional. E.g.

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 an If statement to add any predicates that were actually assigned to:

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

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

So far that seems to work fine in my code.

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

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

I haven't tried that though.

like image 102
tomRedox Avatar answered Sep 22 '22 16:09

tomRedox