简体   繁体   中英

EF Core 2.1 Linq Query to EF Core 3.0

I am dealing with a query that i cannot get to work on ef core 3.0 and it worked fine in the 2.1 version, i would like if anyone can help figure how i can get it work in the 3.0 version.

So let me start with the challenge.

i have a table that is named Zone and it has a one to many relationship to a table called Bin, and bin has a one to many relationship to a table called BinItems, and BinItem has a one to one relationship to a table called PlanItem, and PlanItem has a one to one relationship with a table called Plan.

so i want to get a list of all plans in the plan table, but it must meet the condition that the plan items are in the zone of a specified list of zones. so let say i have a list of ZoneIds and i am looking for all plans that their related bin items is inside that Zones.

so here is my query that used in the 2.1 version and worked until migration to 3.0.

var filterQuery = _PlanRepository.Table;
filterQuery = filterQuery.Where(x => x.Items.Where(o => o.BinItem != null 
                                         && o.BinItem.Bin != null
                                         && o.BinItem.Bin.Zone != null)
                                 .Select(y => y.BinItem.Bin.Zone.Id)
                                 .Intersect(PlanFilter.WarehouseIds).Any());

var finalQuery = filterQuery
                     .Include(x => x.Items)
                           .ThenInclude(x => x.BinItem)
                           .ThenInclude(x => x.Bin)
                           .ThenInclude(x => x.Zone)
                           .AsQueryable();

now this gives me now an error like the following.

System.InvalidOperationException: Processing of the LINQ expression 'Intersect( source1: Select( source: Where( source: AsQueryable(MaterializeCollectionNavigation(Navigation: Plan.Items (k__BackingField, ICollection) Collection ToDependent PlanItem Inverse: Plan, Where( source: NavigationExpansionExpression Source: Where, Bin>, WarehouseSection>>( source: LeftJoin, Bin>, WarehouseSection, Nullable, TransparentIdentifier, Bin>, WarehouseSection>>( outer: LeftJoin, Bin, Nullable, TransparentIdentifier, Bin>>( outer: LeftJoin, TransparentIdentifier>( outer: Where( source: DbSet, predicate: (p0) => Property>((Unhandled parameter: p), "Id") == Property>(p0, "PlanId")), inner: DbSet, outerKeySelector: (p0) => Property>(p0, "BinItemId"), innerKeySelector: (b) => Property>(b, "Id"), resultSelector: (o, i) => new TransparentIdentifier( Outer = o, Inner = i )), inner: DbSet, outerKeySelector: (p0) => Property>(p0.Inner, "BinId"), innerKeySelector: (b0) => Property>(b0, "Id"), resultSelector: (o , i) => new TransparentIdentifier, Bin>( Outer = o, Inner = i )), inner: DbSet, outerKeySelector: (p0) => Property>(p0.Inner, "ZoneId"), innerKeySelector: (w) => Property>(w, "Id"), resultSelector: (o, i) => new TransparentIdentifier, Bin>, WarehouseSection>( Outer = o, Inner = i )), predicate: (p0) => Property>(p0.Outer.Outer.Inner, "Id") != null && Property>(p0.Outer.Inner, "Id") != null && Property>(p0.Inner, "Id") != null) PendingSelector: (p0) => NavigationTreeExpression Value: EntityReferencePlanItem Expression: p0.Outer.Outer.Outer.BinItem.Bin.Zone.Id , predicate: (i) => Property>(NavigationTreeExpression Value: EntityReferencePlan Expression: (Unhandled parameter: p), "Id") == Property>(i, "PlanId")))), predicate: (o) => Property>(o.BinItem, "Id") != null && Property>(o.BinItem.Bin, "Id") != null && Property>(o.BinItem.Bin.Zone, "Id") != null), selector: (y) => y.BinItem.Bin.Zone.Id), source2: (Unhandled parameter: __PlanFilter_WarehouseIds_0))' by 'NavigationExpandingExpressio nVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information. at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ExpandNavigationsInExpression(NavigationExpansionExpression source, Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessWhere(NavigationExpansionExpression source, LambdaExpression predicate)

can anyone share how i can get this to work in Ef core 3.0 version?

ok, i figured it out.

here is my new code that works.

                query = query.Where(x => x.Items.Where(o => o.BinItem != null
                                                     && o.BinItem.Bin != null
                                                     && o.BinItem.Bin.Zone != null
                                                     && PlanFilter.WarehouseIds.Contains(o.BinItem.Bin.Zone.Id)).Any());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM