Search code examples
c#predicatebuilder

PredicateBuilder with DateTime


We've just recently included the PredicateBuilder class in our project and encountered an issue. In the following code, why would the createDateTime condition not included in the generated sql? The other filters are generated properly except for the date. We've also referenced LinqKit, if that matters.

    var predicate = PredicateBuilder.True<Booking>();
    predicate = predicate.And(p => p.propertyId == propertyId).And(p => p.status == (int)status);

    //if (dtFrom != null && dtFrom.HasValue)
    //{
        predicate.And(p => p.createDateTime >= dtFrom.Value);
    //}
    return this.ObjectContext.Bookings.AsExpandable().Where(predicate).OrderBy(p => p.createDateTime).Select(b => b.id).ToList();

Solution

  • You need to assign the predicate back to the variable.

    Code as below:

            predicate = predicate.And(p => p.createDateTime >= dtFrom.Value);