为什么我的EF核心小组通过带有外部联接的查询进行本地评估?

时间:2019-06-05 20:45:07

标签: entity-framework .net-core

我正在尝试使用EF Core 2.2对SQL Server进行按组查询,但是它似乎是在本地执行外部联接部分。

以下是查询:

var payerIds = new int[]{...}
var query = from sr in ctx.DataContext.StatementRecipients
                join pf1 in ctx.DataContext.PaymentFacts on sr.RecipientId equals pf1.PayerId into outerJoinPaymentFacts 
                from pf in outerJoinPaymentFacts.DefaultIfEmpty()
                where  payerIds.Contains(sr.RecipientId)
                group  pf.AmountRemaining by sr.RecipientId
                into g
                select new
                {
                    UserId = g.Key,
                    UnappliedAmountTotal = g.Sum()
                };

警告:

    The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
16:32:28 WRN] The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
[16:32:28 WRN] The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'from PaymentFactEntity pf in {[outerJoinPaymentFacts] => DefaultIfEmpty()}' could not be translated and will be evaluated locally.
[16:32:28 WRN] The LINQ expression 'from PaymentFactEntity pf in {[outerJoinPaymentFacts] => DefaultIfEmpty()}' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'GroupBy([sr].RecipientId, [pf]?.AmountRemaining)' could not be translated and will be evaluated locally.
[16:32:28 WRN] The LINQ expression 'GroupBy([sr].RecipientId, [pf]?.AmountRemaining)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
[16:32:28 WRN] The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
[warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
16:32:28 WRN] The LINQ expression 'DefaultIfEmpty()' could not be translated and will be evaluated locally.
[warn16:32:28: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'from PaymentFactEntity pf in {[outerJoinPaymentFacts] => DefaultIfEmpty()}' could not be translated and will be evaluated locally.
 WRN] The LINQ expression 'from PaymentFactEntity pf in {[outerJoinPaymentFacts] => DefaultIfEmpty()}' could not be translated and will be evaluated locally.
[warn: Microsoft.EntityFrameworkCore.Query[20500]

给什么?

感谢@Gert Arnold提供有关隐式分组的提示。我修改后的查询现在看起来像这样:

但是,正如他所说,它仍在本地进行评估。

  var query = from sr in ctx.DataContext.StatementRecipients
            join pf1 in ctx.DataContext.PaymentFacts on sr.RecipientId equals pf1.PayerId into outerJoinPaymentFacts
            from pf in outerJoinPaymentFacts.DefaultIfEmpty()
            where  payerIds.Contains(sr.RecipientId)
            select new
            {
                UserId = sr.RecipientId,
                UnappliedAmountTotal = outerJoinPaymentFacts.Sum(x => x.AmountRemaining)
            };

以下是我尝试过的其他内容;

1)用单个int payerid值替换了payerid数组。我想知道数组是否在强制进行本地评估。不是。即使当我过滤到单个收款人/付款人ID时,查询仍然会评估语言区域

2)在运行查询之前,我做了一个DbContext.SaveChangesAsync(),以将本地上下文中的所有更改刷新回db。我的理论是,付款事实表存在一些未提交的更改,而这些未提交的更改会强制进行本地评估,因此可以将其包括在结果中。我的理论没有成功,因为查询仍然在本地进行评估。

我们是否可以从EF引擎获得某种方式/诊断,以了解为什么它需要在本地运行这些部分?

0 个答案:

没有答案