不支持嵌套查询。 Operation1 ='UnionAll'Operation2 ='MultiStreamNest'

时间:2012-04-13 09:01:58

标签: 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()它可以正常工作。另外一点,我的一个属性是枚举,但是我使用整数包装器属性来分配它。

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

谢谢,

Ť

3 个答案:

答案 0 :(得分:0)

你试过

吗?
 var results = x.Union(y);

TIZ

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
});

答案 1 :(得分:0)

我在尝试将多组导航属性连接或联合到单个IEnumerable时遇到了类似的问题,这里是代码示例:

var requiredDocuments =                
                 (from x in db.RequestTypes where (some condition) select x.RequiredDocuments)
                 .SelectMany(r => r).ToList<DataModel.RequiredDocument>()
                 .Concat(
                 (from c in db.Categories where (some condition) select c.RequiredDocuments)
                 .SelectMany(r => r).ToList<DataModel.RequiredDocument>()
                 )
                 .Concat(
                 (from f in db.Fields where (some condition) select f.RequiredDocuments)
                 .SelectMany(r => r).ToList<DataModel.RequiredDocument>()
                );

答案 2 :(得分:0)

如果我理解你正在尝试做什么,我会多次遇到同样的问题。最重要的是,不支持使用嵌套投影进行联合,如果您需要这样做,则必须首先使用ToList实现结果。

相关问题