linq - 不支持嵌套查询。操作 1 ='UnionAll' 操作 2 ='MultiStreamNest'

标签 linq entity-framework

我有以下形式的 Linq to Entities 查询:

var x = from a in SomeData
    where ... some conditions ...
    select new MyType
    {
        Property = a.Property,
        ChildCollection = from b in a.Children
                        select new MyChildType
                        {
                            SomeProperty = b.Property,
                            AnotherProperty = b.AnotherProperty
                        }
    };

var y = from a in SomeData
    where ... some other conditions ...
    select new MyType
    {
        Property = a.Property,
        ChildCollection = from b in a.Children
                        select new MyChildType
                        {
                            SomeProperty = b.Property,
                            AnotherProperty = b.AnotherProperty
                        }
    };

var results = x.Concat(y);

(这是一个简化的例子 - 'where' 和 'select' 子句比这里显示的更复杂。我使用单独的查询语句,因为创建一个单独的组合语句太复杂了,有太多的条件,需要很长时间才能编译)

编译正常,但执行失败,异常:
"The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'

请注意,我正在尝试投影到嵌套类型结构中。如果我在 Concat() 之前在 x 和 y 上调用 .ToList() 它工作正常。还有一点,我的一个属性是枚举,但我使用整数包装器属性分配给它。

有没有一种方法可以做我想做的事情而不必将所有数据拉入内存?或者是导致失败的枚举?

谢谢,

最佳答案

你有没有试过

 var results = x.Union(y);

?

蒂兹

或者
var x = (from a in SomeData
where ... some conditions ...
select new MyType
{
    Property = a.Property,
    ChildCollection = (from b in a.Children
                    select new MyChildType
                    {
                        SomeProperty = b.Property,
                        AnotherProperty = b.AnotherProperty
                    }).ToArray() //or DefaultIfEmpty
}).Concat(
from a in SomeData
where ... some other conditions ...
select new MyType
{
    Property = a.Property,
    ChildCollection = (from b in a.Children
                    select new MyChildType
                    {
                        SomeProperty = b.Property,
                        AnotherProperty = b.AnotherProperty
                    }).ToArray() //or DefaultIfEmpty
});

关于linq - 不支持嵌套查询。操作 1 ='UnionAll' 操作 2 ='MultiStreamNest',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10138023/

相关文章:

c# - 为什么 char[].Cast<int> 引发强制转换异常?

c# - Entity Framework - 对业务层需求的意见

entity-framework - 部署到 Azure 后,EF 代码首次迁移未运行

c# - 如何内部连接来自不同数据上下文的表?

.net - 存储库模式和抽象类的问题

c# - 对于这种简单的 Linq 用法,我的代码效率很低

.net - 使用 LINQ 混合 2 个数组

c# - 在以下情况下正确使用抽象类或接口(interface)

entity-framework - 是否可以将 Entity Framework 包装到 odbc?

c# - Entity Framework 代码优先中的数据库初始化