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

Ste*_*ers 9 c# linq linq-to-entities entity-framework

我有一个简单的服务表结构,每个服务都有很多设施.在数据库中,这是一个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但是,删除它会使它工作,这让我相信它只在这个特定的方法调用组合中:

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)

有没有办法完成上述工作:选择一个非实体对象,然后使用WhereGroupBy得到的可查询?ToListSelect工作之后添加,但是大型源集使得这不可行(它将在数据库上执行查询,然后在C#中对逻辑进行分组).

Ger*_*old 13

此异常源自EF源代码中的这段代码......

// <summary>
// Not Supported common processing
// For all those cases where we don't intend to support
// a nest operation as a child, we have this routine to
// do the work.
// </summary>
private Node NestingNotSupported(Op op, Node n)
{
    // First, visit my children
    VisitChildren(n);
    m_varRemapper.RemapNode(n);

    // Make sure we don't have a child that is a nest op.
    foreach (var chi in n.Children)
    {
        if (IsNestOpNode(chi))
        {
            throw new NotSupportedException(Strings.ADP_NestingNotSupported(op.OpType.ToString(), chi.Op.OpType.ToString()));
        }
    }
    return n;
}
Run Code Online (Sandbox Code Playgroud)

我不得不承认:这里发生的事情并不明显,也没有技术设计文件披露所有EF的查询构建策略.但这段代码......

// We can only pull the nest over a Join/Apply if it has keys, so
// we can order things; if it doesn't have keys, we throw a NotSupported
// exception.
foreach (var chi in n.Children)
{
    if (op.OpType != OpType.MultiStreamNest
        && chi.Op.IsRelOp)
    {
        var keys = Command.PullupKeys(chi);

        if (null == keys
            || keys.NoKeys)
        {
            throw new NotSupportedException(Strings.ADP_KeysRequiredForJoinOverNest(op.OpType.ToString()));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在窗帘后面略微窥视一下.我只是尝试了OrderBy一个我自己的案例,完全复制了你的,并且它有效.所以我很确定如果你这样做......

Services
    .Select(s => new { Id = s.Id, Type = s.Type, Facilities = s.Facilities })

    .OrderBy(x => x.Id)

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

例外将消失.