C
C#6mo ago
linqisnice

Polymorphic query handling in EF Core - Thoughts?

So I'm having to deal a lot with entity polymorphism and querying the db to avoid having to execute multiple separate queries. I'm not sure about best practices here, but i also have to eagerload certain nav props from the derived types. Anyone know a better way than this?
private async Task<IReadOnlyList<CoreProduct>> GetCoreProducts(IEnumerable<IProductRequest> requests, int internalPropertyId)
{
var query = _dbContext.Products.OfType<CoreProduct>().AsSplitQuery();

foreach(var request in requests.GroupBy(x => x.GetType(), x => x))
{
query = request switch
{
IAccommodationRequest => AccommodationQuery(query),
// more types here
_ => throw new UnreachableException()
};
}

return await query.Where(x => x.InternalPropertyId == internalPropertyId && requests.Select(x => x.ProductId).Contains(x.Id)).ToListAsync();
}

private IQueryable<CoreProduct> AccommodationQuery(IQueryable<CoreProduct> query)
=> query = query.OfType<InternalAccommodation>().Include(x => x.PriceDetails);
private async Task<IReadOnlyList<CoreProduct>> GetCoreProducts(IEnumerable<IProductRequest> requests, int internalPropertyId)
{
var query = _dbContext.Products.OfType<CoreProduct>().AsSplitQuery();

foreach(var request in requests.GroupBy(x => x.GetType(), x => x))
{
query = request switch
{
IAccommodationRequest => AccommodationQuery(query),
// more types here
_ => throw new UnreachableException()
};
}

return await query.Where(x => x.InternalPropertyId == internalPropertyId && requests.Select(x => x.ProductId).Contains(x.Id)).ToListAsync();
}

private IQueryable<CoreProduct> AccommodationQuery(IQueryable<CoreProduct> query)
=> query = query.OfType<InternalAccommodation>().Include(x => x.PriceDetails);
9 Replies
boiled goose
boiled goose6mo ago
you can use .Include(_ => (_ as InternalAccommodation).PriceDetails)
linqisnice
linqisnice6mo ago
hmm yeah that might be cleaner, but then I have to suppress the null warning on the cast
boiled goose
boiled goose6mo ago
yeah the warning is a little bothering
linqisnice
linqisnice6mo ago
could place the switch in its own class and namespace and use pragma warning disable lol
boiled goose
boiled goose6mo ago
it doesn't look to me that you really need thet switch i mean it depends on how you want to organize the code
linqisnice
linqisnice6mo ago
ur probably right, at that point its just building the query and it doesn't impact performance either way if none of those requests are materialized if they don't exist
boiled goose
boiled goose6mo ago
seems a quite wild query why is not PriceDetails in CoreProduct?
linqisnice
linqisnice6mo ago
beacuse different products have different price details
boiled goose
boiled goose6mo ago
and why a single query is trying to extract such heterogeneous objects/relations?
Want results from more Discord servers?
Add your server
More Posts
How to show validation message for a nested object's property in a list in blazorI have 2 models Customer and Contact. A customer can have 1 or more contacts. I'm using EditForm andmaking an overlayI want to make my own overlay, just like xbox gamebar or that geforce experience thing where you cani dont understand why sometimes this code works and sometimes i get an infinite loop``` public List<List<int>> roomMatrix = new List<List<int>> { new List<int> {-1, -1, Assigning Values to Nested Class MembersI am new to C#. I am wondering how to instantiate an object with nested class members: ```c# namespHow to use Server Sent Events with HttpClientI'm working with an API framework (https://docs.mistral.ai/api/#operation/createChatCompletion) in wHow could I implement the tree correctly using XAML?Hello! I've started a XAML .NET application https://github.com/PerikiyoXD/RWTree/ The problem I'm Forwarding domain emails without a mailboxI have a few domain names on GoDaddy and they charge to have a mailbox, although I don't need the maHow do i send zipefile html with video and imageSo I'm doing a project at school and I am using Visual studio code.. I've usually don't code so i doHaving Trouble setting Outputpath for FrostyHash.vcxprojI'm not entirely sure where to set the output path in the vcxproj due to it looking different to theHow do I set environment variables for a MAUI Android app?at work my team and I have been struggling with how to get values (versions, prod urls, etc.) we sto