我有一个以下形式的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
发布于 2012-04-13 17:07:20
您是否尝试过
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
});发布于 2013-10-21 19:09:51
我在尝试将多组导航属性连接或联合到单个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>()
);发布于 2017-03-03 05:49:32
如果我正确地理解了您要做的事情,我已经多次遇到相同的问题。底线是,不支持使用嵌套投影进行联合,如果需要这样做,则必须首先使用ToList实现结果。
https://stackoverflow.com/questions/10138023
复制相似问题