entity-framework-6entity-framework-plus

IncludeFilter multiple levels not working


Having problems with this IncludeFilter and including multiple levels. I'm using Entity Framework 6 Plus.

This is the example they provide https://entityframework-plus.net/query-include-filter under the Load multiple levels heading.

This works, but it returns more warranty records than I want.

db.Application.Where(x => x.ApplicationId == applicationId)
    .Include(x => x.Lienholder)
    .Include(x => x.Warranty.Select(y => y.Coverage.CoverageGroup))
    .Include(x => x.Vehicle)
    .Include(x => x.Vendor)
    .First();

This works but .Coverage is null, but it's the closest one to the example.

db.Application.Where(x => x.ApplicationId == applicationId)
    .Include(x => x.Lienholder)
    .IncludeFilter(x => x.Warranty.Where(z => true).Where(z =>
        z.WarrantyStatusId == 2 ||
        z.WarrantyStatusId == 3 ||
        z.WarrantyStatusId == 8 ||
        z.WarrantyStatusId == 10
    )
    .IncludeFilter(x => x.Warranty.Where(z => true).Where(z =>
        z.WarrantyStatusId == 2 ||
        z.WarrantyStatusId == 3 ||
        z.WarrantyStatusId == 8 ||
        z.WarrantyStatusId == 10
    ).Select(y => y.Coverage.CoverageGroup))
    .Include(x => x.Vehicle)
    .Include(x => x.Vendor)
    .First();

This doesn't work at all because apparently with IncludeFilter you have to include a where clause

db.Application.Where(x => x.ApplicationId == applicationId)
    .Include(x => x.Lienholder)
    .IncludeFilter(x => x.Warranty.Where(z => true).Where(z =>
        z.WarrantyStatusId == 2 ||
        z.WarrantyStatusId == 3 ||
        z.WarrantyStatusId == 8 ||
        z.WarrantyStatusId == 10
    ).Select(y => y.Coverage.CoverageGroup))
    .Include(x => x.Vehicle)
    .Include(x => x.Vendor)
    .First();

This one works but it doesn't eager load Coverage

db.Application.Where(x => x.ApplicationId == applicationId)
    .Include(x => x.Lienholder)
    .IncludeFilter(x => x.Warranty.Where(z => true).Where(z =>
        z.WarrantyStatusId == 2 ||
        z.WarrantyStatusId == 3 ||
        z.WarrantyStatusId == 8 ||
        z.WarrantyStatusId == 10
    )
    .Include(x => x.Vehicle)
    .Include(x => x.Vendor)
    .First();

Solution

  • In your closest example, you missed to include "Y.Coverage"

    Those are currently limitation of the library.

    Here is what you are looking for:

    db.Application.Where(x => x.ApplicationId == applicationId)
        .IncludeFilter(x => x.Lienholder)
        .IncludeFilter(x => x.Warranty.Where(z =>
            z.WarrantyStatusId == 2 ||
            z.WarrantyStatusId == 3 ||
            z.WarrantyStatusId == 8 ||
            z.WarrantyStatusId == 10
        ))
        .IncludeFilter(x => x.Warranty.Where(z => true).Where(z =>
            z.WarrantyStatusId == 2 ||
            z.WarrantyStatusId == 3 ||
            z.WarrantyStatusId == 8 ||
            z.WarrantyStatusId == 10
        ).Select(y => y.Coverage))
        .IncludeFilter(x => x.Warranty.Where(z => true).Where(z =>
            z.WarrantyStatusId == 2 ||
            z.WarrantyStatusId == 3 ||
            z.WarrantyStatusId == 8 ||
            z.WarrantyStatusId == 10
        ).Select(y => y.Coverage.CoverageGroup)))
        .IncludeFilter(x => x.Vehicle)
        .IncludeFilter(x => x.Vendor)
        .First();
    

    This doesn't work at all because apparently with IncludeFilter you have to include a where clause

    No, you don't have.