Client side GroupBy is not supported

↘锁芯ラ 提交于 2019-12-05 17:24:48

问题


I have the following Entity Framework Core 3.0:

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));

And I get the following error:

Client side GroupBy is not supported.

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

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));

Now it works ...


回答1:


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.

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




回答2:


Apparently it's not yet supported in EF Core 3.0, that is, it is not supported to perform GroupBy over SQL.

One possible solution (works for me) is to make the GroupBy on a List object.

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


来源:https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!