相关疑难解决方法(0)

为什么Select,Where和GroupBy的这种组合会导致异常?

我有一个简单的服务表结构,每个服务都有很多设施.在数据库中,这是一个Service表和一个Facility表,其中Facility表具有对Service表中的行的引用.

在我们的应用程序中,我们有以下LINQ工作:

Services
    .Where(s => s.Facilities.Any(f => f.Name == "Sample"))
    .GroupBy(s => s.Type)
    .Select(g => new { Type = g.Key, Count = g.Count() })
Run Code Online (Sandbox Code Playgroud)

但由于我无法控制的原因,源集在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() })
Run Code Online (Sandbox Code Playgroud)

但这引发了以下异常,没有内部异常:

EntityCommandCompilationException: The nested query is not supported. Operation1='GroupBy' Operation2='MultiStreamNest'

Where …

c# linq linq-to-entities entity-framework

9
推荐指数
1
解决办法
2258
查看次数

标签 统计

c# ×1

entity-framework ×1

linq ×1

linq-to-entities ×1