7

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

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

谢谢,

4

3 回答 3

0

你试过吗

 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
});
于 2012-04-13T09:07:19.830 回答
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>()
                );
于 2013-10-21T11:09:50.820 回答
0

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

于 2017-03-02T21:49:31.903 回答