Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么Select、Where和GroupBy的组合会导致异常?_C#_Linq_Entity Framework_Linq To Entities - Fatal编程技术网

C# 为什么Select、Where和GroupBy的组合会导致异常?

C# 为什么Select、Where和GroupBy的组合会导致异常?,c#,linq,entity-framework,linq-to-entities,C#,Linq,Entity Framework,Linq To Entities,我有一个简单的服务表结构,每个表都有一些设施。在数据库中,这是一个服务表和一个设施表,其中设施表引用了服务表中的一行 在我们的应用程序中,我们有以下LINQ工作: Services .Where(s => s.Facilities.Any(f => f.Name == "Sample")) .GroupBy(s => s.Type) .Select(g => new { Type = g.Key, Count = g.Count() }) 但由于

我有一个简单的服务表结构,每个表都有一些设施。在数据库中,这是一个服务表和一个设施表,其中设施表引用了服务表中的一行

在我们的应用程序中,我们有以下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:不支持嵌套查询。Operation1='GroupBy'Operation2='MultistreamTest'

但是,删除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和GroupBy?在Select之后添加ToList是可行的,但由于源代码集太大,因此无法执行此操作。它将在数据库上执行查询,然后在C中执行分组逻辑。

此异常源于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;
}
在窗帘后面瞥了一眼。我刚刚在我自己的一个箱子里试了一个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() })

例外情况将消失。

我们讨论的是哪个数据库和EF版本?SQL Server 2014和Entity Framework 6.1.1!让这个问题成为我的一部分。这完全解决了这个问题,太棒了!感谢您深入研究源代码,考虑到异常情况,我不知道该去哪里查找。
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() })