首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不支持嵌套查询。Operation1='UnionAll‘Operation2='MultiStreamNest’

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

Stack Overflow用户
提问于 2012-04-13 17:01:58
回答 3查看 4.3K关注 0票数 7

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

代码语言:javascript
复制
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’子句比这里显示的复杂。我使用单独的查询语句,因为创建一个组合的语句太复杂了,有太多的条件,并且需要很长时间才能编译)

编译正常,但在执行时失败,并出现异常:

代码语言:javascript
复制
"The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'

注意,我正在尝试投射到一个嵌套的类型化结构中。如果我在Concat()之前调用x和y上的.ToList(),它工作得很好。另外,我的一个属性是枚举,但我使用整数包装器属性将其赋值给它。

有没有一种方法,我可以做我想做的事情,而不必把所有的数据都放到内存中?或者是枚举导致了失败?

谢谢,

T

EN

回答 3

Stack Overflow用户

发布于 2012-04-13 17:07:20

您是否尝试过

代码语言:javascript
复制
 var results = x.Union(y);

Tiz

代码语言:javascript
复制
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
});
票数 0
EN

Stack Overflow用户

发布于 2013-10-21 19:09:51

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

代码语言:javascript
复制
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>()
                );
票数 0
EN

Stack Overflow用户

发布于 2017-03-03 05:49:32

如果我正确地理解了您要做的事情,我已经多次遇到相同的问题。底线是,不支持使用嵌套投影进行联合,如果需要这样做,则必须首先使用ToList实现结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10138023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档