在具有“不支持客户端GroupBy”的EF 3.x中,最简单的分组依据失败。 [英] Simplest Group By Fails in EF 3.x with "Client side GroupBy is not supported"

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

问题描述

当前正在使用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将不再允许客户端GroupBy,除非在顶层投影中。

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/zh-cn/ef/core/what-是新的/ef-core-3.0/breaking-changes#linq查询不再在客户端上进行评估


旧行为

在3.0之前,当EF Core无法转换属于$ b $的表达式时对SQL或参数进行查询的b,它会自动评估
客户端上的表达式。默认情况下,客户对
可能昂贵的表达式的评估只会触发警告。

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中进行评估?

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中进行评估?我的示例的GroupB不是最高级别的投影吗?

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核心加载和分组

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.

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

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