EF7和GroupBy()不能翻译



我有以下代码在EF7 Beta 8上运行:

var locationGrops = from l in db.Locations
                    group l by l.ServiceType into g
                    select g;
var list = locationGrops.ToList();

当我执行这段代码时,EF显示一个警告。

warning : [Microsoft.Data.Entity.Query.QueryCompilationContext] The LINQ express
ion 'GroupBy([l].ServiceType, [l])' could not be translated and will be evaluate
d locally.

查询对我来说似乎很基本,SQL中有GROUP BY。有没有办法让它在服务器上运行?

目前EF7不支持group by和大多数子查询。

您可以使用context.Locations.FromSql(sql).ToList()来确保您的查询在服务器上按您希望的方式运行。

一种方法是创建一个具有逻辑的数据库视图(对于复杂的分组可能更好)。

现在使用视图有点麻烦,但如果你正在使用脚手架,下面是我的建议:

如何在EF7/Entity Framework Core 1.0中创建DbContext中的数据库视图

总之,我继承了DbContext,并为视图手动创建了实体类。

最新更新