C#C
C#9mo ago
Tvde1

abstract methods inside EF core queries

I'm trying to model my domain as follows
(some EF configuration stuff is left out for brevity)
class Contract
{
    public List<PaymentAttempt> PaymentAttempts { get; }
}

abstract class PaymentAttempt // uses tpc
{
    public abstract bool HasPaid(int amount);
}

class BankPaymentAttempt : PaymentAttempt
{
    public string BankAccount { get; set; }
    public int AmountPaid { get; set; }
    
    public bool HasPaid(int amount) => AmountPaid >= amount;
}

class VipUserPaymentAttempt : PaymentAttempt
{
    public bool IsSuperVip { get; set; }

    public bool HasPaid(int amount) => IsSuperVip;
}


I am looking to do a query like:
var outstandingBalance = 123;
context.Contracts
    .Where(contract => contract.PaymentAttempts
        .Any(attempt => attempt.HasPaid(outstandingBalance)
    );

EF is not able to translate this query unfortunately
Is there a method to handle this? I thought about returning expression trees manually but that seems not very easy
Was this page helpful?