LINQKit
LINQKit copied to clipboard
PredicateBuilder - InvalidOperationException for navigation property
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