Search code examples
c#dapper-extensions

Predicate comparing two fields with DapperExtensions


I am trying to create what seems like a relatively simple predicate statement for Dapper-Extensions in C# after doing a good number of these, but in one case I need to compare two fields rather than a field and a fixed object value:

multiPred.Add<ChargingProfile>(new PredicateGroup
{
    Operator = GroupOperator.And,
    Predicates = new List<IPredicate>
    {
        Predicates.Field<ChargingProfile>(f => f.EndDt, Operator.Eq, null, true),

        // the below statement should check if f.NextChargeDt is greater than f.EndDt
        // (string value is obviously not correct, but to illustrate)
        Predicates.Field<ChargingProfile>(f => f.NextChargeDt, Operator.Gt, "f.EndDt")
    }
});

I can't (or don't know how to) access the expression in the value parameter, so there must be some other way of doing this?

Thanks for any insights you can offer.


Solution

  • You can use Property for creating a predicate:

    var predicate = Predicates.Property<TwoFieldsTable, TwoFieldsTable>(f => f.Field1, Operator.Eq, f2 => f2.Field2);
    var res = conn.GetList<TwoFieldsTable>(predicate);