繁体   English   中英

EF Core 2.1 Linq 查询到 EF Core 3.0

[英]EF Core 2.1 Linq Query to EF Core 3.0

我正在处理一个我无法在 ef core 3.0 上工作的查询,它在 2.1 版本中运行良好,我想如果有人能帮助我弄清楚如何让它在 3.0 版本中工作。

所以让我从挑战开始。

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

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

所以这是我在 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();

现在这给了我如下错误。

System.InvalidOperationException: 处理 LINQ 表达式 'Intersect( source1: Select( source: Where( source: AsQueryable(MaterializeCollectionNavigation(Navigation: Plan.Items (k__BackingField, ICollection) Collection ToDependent PlanItem Inverse: Plan, Where( source: NavigationExpansionExpression Source:其中,Bin>,WarehouseSection>>(来源:LeftJoin,Bin>,WarehouseSection,Nullable,TransparentIdentifier,Bin>,WarehouseSection>>(外层:LeftJoin,Bin,Nullable,TransparentIdentifier,Bin>>(外层:LeftJoin,TransparentIdentifier>(外部:其中(来源:DbSet,谓词:(p0)=>属性>((未处理的参数:p),“Id”)==属性>(p0,“PlanId”)),内部:DbSet,outerKeySelector:(p0 ) => Property>(p0, "BinItemId"), innerKeySelector: (b) => Property>(b, "Id"), resultSelector: (o, i) => new TransparentIdentifier( Outer = o, Inner = i ) ),内部: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 值:EntityReferencePlanItem 表达式:p0.Outer.Outer.Outer.BinItem.Bin.Zone.Id ,谓词:(i) => 属性>(NavigationTreeExpression 值:EntityReferencePlan 表达式:(未处理的参数:p),“ Id") == Property>(i, "PlanId")))),谓词:(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: (未处理的参数: __PlanFilter_WarehouseIds_0))' by 'NavigationExpandingExpressio nVisitor' 失败。 这可能表示 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.VisitMethodCall(MethodCallExpression methodCallExpression) ) 在 Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessWhere(NavigationExpansionExpression source, LambdaExpression predicate)

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

好的,我想通了。

这是我的新代码。

                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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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