本文介绍了不支持客户端GroupBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Entity Framework Core 3.0查询:

I have the following Entity Framework Core 3.0 query:

var units = await context.Units
  .SelectMany(y => y.UnitsI18N)
  .OrderBy(y => y.Name)
  .GroupBy(y => y.LanguageCode)
  .ToDictionaryAsync(y => y.Key, y => y.Select(z => z.Name));

我收到以下错误:

Client side GroupBy is not supported.

要在客户端(或客户端的一部分)上运行查询,我将执行以下操作:

To run the query on the client, or part of it, I would do the following:

var units = context.Units
  .SelectMany(y => y.UnitsI18N)
  .OrderBy(y => y.Name)
  .AsEnumerable()
  .GroupBy(y => y.LanguageCode)
  .ToDictionary(y => y.Key, y => y.Select(z => z.Name));

现在可以使用了.

如果未在客户端上运行查询,为什么会出现此错误?

Why am I getting this error if I am not running the query on the client?

推荐答案

您的.GroupBy(y => y.LanguageCode).ToDictionaryAsync(y => y.Key, y => y.Select(z => z.Name));无法转换为SQL.EF Core 3.0将引发异常,以确保您知道在分组并映射到Dictionary之前将从数据库中提取Units中的所有记录.

Your .GroupBy(y => y.LanguageCode).ToDictionaryAsync(y => y.Key, y => y.Select(z => z.Name)); cannot be converted to SQL.EF Core 3.0 will throw exception to make sure you know that all records in Units will be fetched from database before grouping and map to Dictionary.

这是EF Core 3.0中的重大突破. https://docs .microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes

It's top breaking change in EF Core 3.0.https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes

这篇关于不支持客户端GroupBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:24