我有一个简单的服务表结构,每个服务都有多个设施。在数据库中,这是一个Service
表和一个Facility
表,其中Facility
表引用了服务表中的一行。
在我们的应用程序中,我们有以下 LINQ 工作:
Services
.Where(s => s.Facilities.Any(f => f.Name == "Sample"))
.GroupBy(s => s.Type)
.Select(g => new { Type = g.Key, Count = g.Count() })
但由于我无法控制的原因,源集在Where
调用,这样:
Services
.Select(s => new { Id = s.Id, Type = s.Type, Facilities = s.Facilities })
.Where(s => s.Facilities.Any(f => f.Name == "Sample"))
.GroupBy(s => s.Type)
.Select(g => new { Type = g.Key, Count = g.Count() })
但这会引发以下异常,没有内部异常:
EntityCommandCompilationException: The nested query is not supported. Operation1='GroupBy' Operation2='MultiStreamNest'
删除Where
然而,使它起作用,这让我相信它只在方法调用的这种特定组合中:
Services
.Select(s => new { Id = s.Id, Type = s.Type, Facilities = s.Facilities })
//.Where(s => s.Facilities.Any(f => f.Name == "Sample"))
.GroupBy(s => s.Type)
.Select(g => new { Type = g.Key, Count = g.Count() })
有没有办法使上述工作:选择一个非实体对象,然后使用Where
and GroupBy
关于结果可查询?添加ToList
之后Select
可行,但大型源集使得此操作不可行(它将在数据库上执行查询,然后在 C# 中执行分组逻辑)。