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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With