我有以下实体框架核心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));
我得到以下错误:
Client side GroupBy is not supported.
要在客户机上或其中的一部分上运行查询,我将执行以下操作:
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));
现在起作用了。
如果我没有在客户端上运行查询,为什么会得到这个错误?
发布于 2021-10-29 19:33:02
客户端组支持
用EF Core 3.1.15.0测试
以下代码返回Client side GroupBy is not supported.
错误:
MyEntity
.GroupBy(x => x.MyProperty)
.ToDictionaryAsync(x => x.Key, x => x.Count())
.Dump();
但出于某种原因,您可以在.Select()
之后添加一个.GroupBy()
,它编译并运行预期的.GroupBy()
:
MyEntity
.GroupBy(x => x.MyProperty)
.Select(g => new { Key = g.Key, Count = g.Count() })
.ToDictionaryAsync(x => x.Key, x => x.Count)
.Dump();
汇编成:
SELECT [t].[MyProperty] AS [Key], COUNT(*) AS [Count]
FROM [dbo].[MyEntity] AS [t]
GROUP BY [t].[MyProperty]
https://stackoverflow.com/questions/58138556
复制相似问题