【问题标题】:The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'不支持嵌套查询。 Operation1='UnionAll' Operation2='MultiStreamNest'
【发布时间】:2012-04-13 09:01:57
【问题描述】:

我有一个如下形式的 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() 它工作正常。另外一点,我的属性之一是枚举,但我使用整数包装器属性对其进行分配。

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

谢谢,

T

【问题讨论】:

    标签: linq entity-framework


    【解决方案1】:

    你试过了吗

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

    【讨论】:

    • 刚刚尝试了其中的第二个,它的行为似乎与第一个完全相同,即没有像我希望的那样工作(请参阅我对 @Arion 帖子的评论。
    • ...现在已被删除。无论如何,不​​要介意使用 Union,因为结果集无论如何都应该是不同的,但是我得到了异常“'Distinct' 操作不能应用于指定参数的集合 ResultType。”我认为这是因为 Distinct 无法处理嵌套集合。
    • 如果为空?它的行为如何?
    • 如果什么是空的?如果我注释掉 ChildCollection 元素的投影,它可以正常工作。谢谢你顺便看看这个:-)
    • MyType.ChildCollection 的类型可能是 IEnumerable(of MyChildType) 是什么??
    【解决方案2】:

    我在尝试将多组导航属性连接或合并到单个 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>()
                    );
    

    【讨论】:

    • 这是您有问题的代码还是有效的代码?
    • 因为这会在执行 Concat 之前调用 ToList 这应该可以工作。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多