EF 3.x 中最简单的 Group By 失败,“不支持客户端 GroupBy" [英] Simplest Group By Fails in EF 3.x with "Client side GroupBy is not supported"

查看:35
本文介绍了EF 3.x 中最简单的 Group By 失败,“不支持客户端 GroupBy"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用 EF Core 3.1.1 版进行测试.

Currently testing with EF Core version 3.1.1.

当我搜索时,我找到了对这个问题的引用,但我没有看到任何关于为什么会发生这种情况的明确答案,以及它是一个将被修复的错误还是它的预期行为.这就像看起来一样简单——BatchRequest 表/实体有一个名为 BatchId 的整数列/属性:

When I search, I find references to this problem, but I don't see any definitive answers about why this happens, and whether it is a bug that will be fixed or if it's expected behavior. This is just as simple as it looks--the BatchRequest table/entity has an integer column/property called BatchId:

var batchRequestGroups = context.BatchRequests.GroupBy(br => br.BatchId).ToList();

当我运行这个时,我得到一个 System.InvalidOperationException,不支持客户端 GroupBy."

When I run this, I get a System.InvalidOperationException, "Client side GroupBy is not supported."

因此,根据我在网上找到的解释,EF Core 3.x 似乎不再允许客户端 GroupBys,除非在顶级投影中.

So, based on the explanations that I find online, it appears that EF Core 3.x will no longer allow client-side GroupBys except in the top-level projection.

来自 https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/break-更改#linq-queries-are-no-longer-evaluated-on-the-client:

旧行为

在 3.0 之前,当 EF Core 无法转换作为一部分的表达式时对 SQL 或参数的查询,它会自动评估客户端上的表达式.默认情况下,客户端评估潜在开销较大的表达式只会触发警告.

Before 3.0, when EF Core couldn't convert an expression that was part of a query to either SQL or a parameter, it automatically evaluated the expression on the client. By default, client evaluation of potentially expensive expressions only triggered a warning.

新行为

从 3.0 开始,EF Core 只允许顶层的表达式要评估的投影(查询中的最后一个 Select() 调用)客户端.当查询的任何其他部分中的表达式不能为转换为 SQL 或参数,抛出异常.

Starting with 3.0, EF Core only allows expressions in the top-level projection (the last Select() call in the query) to be evaluated on the client. When expressions in any other part of the query can't be converted to either SQL or a parameter, an exception is thrown.

这是否意味着在以前的版本中,ALL GroupBy 子句是在客户端上评估的?

Does this mean that in previous versions, ALL GroupBy clauses were evaluated on the client?

另外,为什么我的示例中的 GroupBy 不能在 SQL 中进行评估?我的示例的 GroupBy 怎么不是顶级投影?

Also, why can't the GroupBy in my example be evaluated in SQL? And how is my example's GroupBy not the top-level projection?

再次,我的主要问题是:这是一个错误还是预期的行为?

Again, my main question: is this a bug or expected behavior?

推荐答案

从 EF 3.0 开始,客户端对查询的评估被视为错误,这与将其视为警告的先前版本不同.

Starting from EF 3.0 clientside evaluations of queries are treated as an error, unlike the previous versions where it was treated as a warning.

这是否意味着在以前的版本中,所有 GroupBy 子句都在客户端上进行评估?

Does this mean that in previous versions, ALL GroupBy clauses were evaluated on the client?

没有.并非所有 group by 语句都在客户端进行评估.这取决于 EF 核心是否能够将 LINQ 转换为支持的 SQL 语句.

NO. Not all of the group by statements were being evaluated on clientside. It depends on whether the EF core was able to translate the LINQ to a supported SQL statement.

另外,为什么我的示例中的 GroupBy 不能在 SQL 中进行评估?我的示例的 GroupBy 怎么不是顶级投影?

Also, why can't the GroupBy in my example be evaluated in SQL? And how is my example's GroupBy not the top-level projection?

我的猜测是您的实体包含其他关系和 EF 核心加载和按指定属性分组,而这些在 SQL 中无法完成.

My guess is your entity contains other relationships and EF core loading and grouping by specified property which can't be done in SQL.

再次,我的主要问题是:这是一个错误还是预期的行为?

Again, my main question: is this a bug or expected behavior?

这显然不是错误.这是预期的行为.

It's not a bug apparently. This expected behaviour.

这篇关于EF 3.x 中最简单的 Group By 失败,“不支持客户端 GroupBy"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆