EF Core 2.1对EF Core 3.0的Linq查询

时间:2019-12-04 18:59:38

标签: c# entity-framework linq entity-framework-core ef-core-3.0

我正在处理一个查询,我无法在ef core 3.0上工作,并且在2.1版本中可以正常工作,我希望有人可以帮助我确定如何使其在3.0版中工作。

所以让我从挑战开始。

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

所以我想获得计划表中所有计划的列表,但是它必须满足以下条件:计划项位于指定区域列表的区域中。因此,假设我有一个ZoneIds列表,并且我正在寻找与其相关bin项目位于该区域内的所有计划。

这是我在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:选择(           资料来源:               源:AsQueryable(MaterializeCollectionNavigation(导航:Plan.Items(k__BackingField,ICollection)Collection ToDependent PlanItem逆:计划,其中(                   来源:NavigationExpansionExpression                       来源:Where,Bin>,WarehouseSection >>(                           来源:LeftJoin,Bin>,WarehouseSection,可为空,TransparentIdentifier,Bin>,WarehouseSection >>(                               外部:LeftJoin,Bin,可为空,TransparentIdentifier,Bin >>(                                   外部:LeftJoin,TransparentIdentifier>(                                       外部:在哪里                                           来源:DbSet,                                           谓词:(p0)=>属性>((未处理的参数:p),“ Id”)==属性>(p0,“ PlanId”)),                                       内部:DbSet,                                       externalKeySelector:(p0)=>属性>(p0,“ BinItemId”),                                       innerKeySelector:(b)=>属性>(b,“ Id”),                                       resultSelector:(o,i)=>新的TransparentIdentifier(                                           外层= o,                                           内在=我                                       )),                                   内部:DbSet,                                   externalKeySelector:(p0)=>属性>(p0.Inner,“ BinId”),                                   innerKeySelector:(b0)=>属性>(b0,“ Id”),                                   resultSelector:(o,i)=>新的TransparentIdentifier,Bin>(                                       外层= o,                                       内在=我                                   )),                               内部:DbSet,                               externalKeySelector:(p0)=>属性>(p0.Inner,“ ZoneId”),                               innerKeySelector:(w)=>属性>(w,“ Id”),                               resultSelector:(o,i)=>新的TransparentIdentifier,Bin>,WarehouseSection>(                                   外层= o,                                   内在=我                               )),                           谓词:(p0)=>属性>(p0.Outer.Outer.Inner,“ Id”)!= null &&属性>(p0.Outer.Inner,“ Id”)!= null &&属性>(p0.Inner, “ Id”)!= null)                       PendingSelector:(p0)=>导航树表达式                           值:EntityReferencePlanItem                           表达式:p0.Outer.Outer.Outer.BinItem.Bin.Zone.Id                   ,                   谓词:(i)=>属性>(NavigationTreeExpression                       值:EntityReferencePlan                       表达式:(未处理的参数:p),“ Id”)==属性>(i,“ PlanId”))))),               谓词:(o)=>属性>(o.BinItem,“ Id”)!= null &&属性>(o.BinItem.Bin,“ Id”)!= null &&属性>(o.BinItem.Bin.Zone, “ Id”)!= null),           选择器:(y)=> y.BinItem.Bin.Zone.Id),       “ NavigationExpandingExpressionVisitor”的“ source2 :(未处理的参数:__ PlanFilter_WarehouseIds_0))”失败。这可能表示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版本中工作吗?

1 个答案:

答案 0 :(得分:2)

好,我知道了。

这是我的新代码。

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