EF Core 2.1 到 EF Core 3.0 的 Linq 查询
EF Core 2.1 Linq Query to EF Core 3.0
我正在处理一个查询,我无法在 ef core 3.0 上工作,但它在 2.1 版本中工作正常,我想知道是否有人可以帮助我了解如何在 3.0 版本中工作。
所以让我从挑战开始。
我有一个名为 Zone 的 table,它与名为 Bin 的 table 具有一对多关系,而 bin 与 table 具有一对多关系称为 BinItems,BinItem 与称为 PlanItem 的 table 具有一对一关系,而 PlanItem 与称为 Plan.table 具有一对一关系。
所以我想获取计划table中所有计划的列表,但它必须满足计划项目在指定区域列表的区域中的条件。所以假设我有一个 ZoneIds 列表,我正在寻找所有计划,他们的相关 bin 项目在那个 Zones 内。
所以这是我在 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: 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 'NavigationExpandingExpressionVisitor' 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)
任何人都可以分享我如何让它在 Ef 核心 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());
我正在处理一个查询,我无法在 ef core 3.0 上工作,但它在 2.1 版本中工作正常,我想知道是否有人可以帮助我了解如何在 3.0 版本中工作。
所以让我从挑战开始。
我有一个名为 Zone 的 table,它与名为 Bin 的 table 具有一对多关系,而 bin 与 table 具有一对多关系称为 BinItems,BinItem 与称为 PlanItem 的 table 具有一对一关系,而 PlanItem 与称为 Plan.table 具有一对一关系。
所以我想获取计划table中所有计划的列表,但它必须满足计划项目在指定区域列表的区域中的条件。所以假设我有一个 ZoneIds 列表,我正在寻找所有计划,他们的相关 bin 项目在那个 Zones 内。
所以这是我在 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: 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 'NavigationExpandingExpressionVisitor' 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)
任何人都可以分享我如何让它在 Ef 核心 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());