© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
25 replies
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;
}
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)
    );
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Mocking EF Core extensions methods.
C#CC# / help
3y ago
EF Core
C#CC# / help
2y ago
Dynamic EF Core LINQ Queries for API Consumption
C#CC# / help
7mo ago