EF Core 2.1 Linq 查询到 EF Core 3.0

Sol*_*ein 1 c# linq entity-framework entity-framework-core ef-core-3.0

我正在处理一个查询,我无法在 ef core 3.0 上工作,它在 2.1 版本中工作得很好,我希望是否有人可以帮助我弄清楚如何让它在 3.0 版本中工作。

那么让我从挑战开始吧。

我有一个名为 Zone 的表,它与名为 Bin 的表具有一对多关系,bin 与名为 BinItems 的表具有一对多关系,BinItem 与名为 PlanItem 的表具有一对一关系, PlanItem 与名为 Plan 的表具有一对一的关系。

所以我想获取计划表中所有计划的列表,但它必须满足计划项位于指定区域列表的区域中的条件。假设我有一个 ZoneId 列表,并且我正在查找其相关垃圾箱项目位于该区域内的所有计划。

所以这是我在 2.1 版本中使用的查询,直到迁移到 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();
Run Code Online (Sandbox Code Playgroud)

现在这给了我一个如下的错误。

(i) => 属性>(NavigationTreeExpression 值:EntityReferencePlan 表达式:(未处理的参数:p), "Id") == 属性>(i, "PlanId")))),谓词:(o) => 属性>( o.BinItem, "Id") != null && Property>(o.BinItem.Bin, "Id") != null && Property>(o.BinItem.Bin.Zone, "Id") != null), 选择器: (y) => y.BinItem.Bin.Zone.Id), source2: (未处理的参数: __PlanFilter_WarehouseIds_0))' by 'NavigationExpandingExpressionVisitor' 失败。这可能表明 EF Core 中存在错误或限制。看https://go.microsoft.com/fwlink/?linkid=2101433了解更多详细信息。在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) 在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) 在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ExpandNavigationsInExpression(NavigationExpansionExpression 源,表达式表达式) )在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessWhere(NavigationExpansionExpression 源,LambdaExpression 谓词)

谁能分享我如何让它在 Ef core 3.0 版本中工作?

Sol*_*ein 5

好吧,我明白了。

这是我有效的新代码。

                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());
Run Code Online (Sandbox Code Playgroud)