Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PredicateBuilder - InvalidOperationException for navigation property #119

Open
thiagomoises opened this issue Jun 29, 2020 · 0 comments
Open

Comments

@thiagomoises
Copy link

Hi,
I am using PredicateBuilder to create a custom filter in my application.
If I add a filter with a navigation property, I have an InvalidOperationException

The LINQ expression 'DbSet<Announcement>
    .Join(
        outer: DbSet<AutoPart>, 
        inner: a => EF.Property<Nullable<Guid>>(a, "AutoPartId"), 
        outerKeySelector: a0 => EF.Property<Nullable<Guid>>(a0, "Id"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<Announcement, AutoPart>(
            Outer = o, 
            Inner = i
        ))
    .Join(
        outer: DbSet<VehicleModel>, 
        inner: a => EF.Property<Nullable<Guid>>(a.Outer, "VehicleModelId"), 
        outerKeySelector: v => EF.Property<Nullable<Guid>>(v, "Id"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<TransparentIdentifier<Announcement, AutoPart>, VehicleModel>(
            Outer = o, 
            Inner = i
        ))
    .Where(a => !(a.Outer.Outer.IsDeleted) && (int)a.Outer.Outer.Status == 0 && a.Outer.Outer.Title.ToString().ToLower().Contains(__ToLower_0) || a.Outer.Inner.Description.ToString().ToLower().Contains(__ToLower_0) || a.Inner.Description.ToString().ToLower().Contains(__ToLower_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

It's my code:

var predicate = PredicateBuilder.New<Announcement>(x => !x.IsDeleted && x.Status == Enums.AnnouncementStatus.Listed);
predicate.And(x => x.Title.ToString().ToLower().Contains(filter["term"].ToString().ToLower())
                //if I remove this list everything works
                || x.AutoPart.Description.ToString().ToLower().Contains(filter["term"].ToString().ToLower()) ||x.VehicleModel.Description.ToString().ToLower().Contains(filter["term"].ToString().ToLower())
                );

var announcements = this.DbContext.Set<Announcement>()
                        .AsExpandableEFCore()
                        .Include(x => x.AutoPart)
                        .Include(x => x.VehicleModel)
                        .Where(predicate)
                        .OrderByDescending(x => x.PostDate)
                        .Skip(Math.Max(pageIndex - 1, 0) * pageSize)
                        .Take(pageSize);

if I remove:

x.AutoPart.Description.ToString().ToLower().Contains(filter["term"].ToString().ToLower()) 

and

x.VehicleModel.Description.ToString().ToLower().Contains(filter["term"].ToString().ToLower())

everything works normally

Environment:
NetCore 3.1
LinqKit.Microsoft.EntityFrameworkCore 3.0.0
Microsoft.EntityFrameworkCore 3.1.4

Tanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant