Open Liero opened 3 years ago
@Liero this is by design. Include only applies to entities that are directly projected in the final result. Because you are using DTO, the entity on which include is defined is not present in the final result and the include is completely ignored.
You can get the desired result by applying the filter directly to the ExportVýrobky
collection in the final projection instead:
dbContext.Export.AsNoTracking()
.Where(e => e.Zákazník == customerId)
.Select(e => new DeliveryNote
{
Id = e.Id,
Date = e.Dátum.Value,
DeliveryService = e.Shipper.Názov,
Invoice = e.Faktúra,
PackageSize = e.Balenie,
TrackingNumber = e.Awb,
Items = e.ExportVýrobky.Where(i => !EF.Functions.Like(i.Výrobok.Zákazka.Objednávka, "%-1%")).Select(item => new DeliveryNoteItem
{
Count = item.Množstvo,
Descrition = item.Výrobok.Popis,
OrderId = item.IdVýrobku ?? 0,
CustomerOrderId = item.Výrobok.Objednávka,
}).ToList()
})
.OrderByDescending(e => e.Date);
assigning myself to fix the documentation - this is not the first time users were expecting include to be applied on entity which is not projected directly, we should make an explicit note about this
note to self: also consider mentioning this scenario: https://github.com/dotnet/efcore/issues/26327
File a bug
GIVEN I use filtered Include
WHEN I use blog.Posts.Select() in a Select clause
THEN the where condition is ignored in the generated SQL
I've not noticed this behavior in the docs so I guess it is unexpected / bug.
Include your code
here is exactly the code and generated SQL.
Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: (NET 5.0 Operating system: Windows 10