【发布时间】:2020-01-03 11:03:18
【问题描述】:
我有一个要求
DbContext.Invoices
.Where(x => x.Status != InvoiceStatus.Draft && x.PaymentMethod == PaymentMethod.DirectDebit)
.Where(x => x.DirectDebitFile == null).ToList();
DirectDebitFile 是反向导航属性。
在 EF Core 2 中运行良好,不确定在最终请求中是如何评估的。 升级到 EF Core 3 后,此请求不再起作用并显示
System.InvalidOperationException: The LINQ expression 'DbSet<Invoice>
.Where(i => !(i.IsDeleted))
.Where(i => i.ClubId == __CurrentUser_ClubId_0)
.Cast()
.Where(e => e.FederationId == __CurrentUser_FederationId_1)
.Cast()
.Where(e0 => !(e0.Hidden))
.Cast()
.Where(e1 => (int)e1.Status != 0 && (Nullable<int>)e1.PaymentMethod == (Nullable<int>)DirectDebit)
.LeftJoin(
outer: DbSet<DirectDebitFile>
.Where(d => !(d.IsDeleted)),
inner: e1 => EF.Property<Nullable<long>>(e1, "Id"),
outerKeySelector: d => EF.Property<Nullable<long>>(d, "InvoiceId"),
innerKeySelector: (o, i) => new TransparentIdentifier<Invoice, DirectDebitFile>(
Outer = o,
Inner = i
))
.Where(e1 => e1.Inner == null)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我可以重写此查询并通过将评估移至客户端使其工作
DbContext.Invoices
.Include(x=>x.DirectDebitFile)
.Where(x => x.Status != InvoiceStatus.Draft && x.PaymentMethod == PaymentMethod.DirectDebit)
.AsEnumerable()
.Where(x => x.DirectDebitFile == null).ToList();
但在这种情况下,当然,查询会提取所有行,并且过滤 x.DirectDebitFile == null 将在客户端进行。我希望在服务器上评估此查询,请帮助实现。
【问题讨论】: