我想知道如何在Linq投影中设置EntityCollection。下面是我的代码:
var orders = (from order in context.Orders
select new
{
reference = order.reference,
pe = order.OrderExecutions //this is an EntityCollection
})
.AsEnumerable()
.Select(o =>
(Orders)new Orders
{
reference = o.reference,
OrderExecutions = o.pe
}
).ToList().AsQueryable();(这段代码看起来很奇怪,但为了在telerik网格中工作,它需要这样的代码)
指令OrderExecutions = o.pe涉及以下错误:
EntityCollection已被初始化。只应在对象图反序列化期间调用InitializeRelatedCollection方法来初始化新的EntityCollection。
OrderExecutions是对象顺序中包含的EntityCollection。
如何避免此错误?有什么想法吗?
我应该修改Order对象中生成的代码吗?
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("PModel", "FK__orderExec__refer__70DDC3D8", "OrderExecutions")]
public EntityCollection<OrderExecutions> OrderExecutions
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<OrderExecutions>("PModel.FK__orderExec__refer__70DDC3D8", "OrderExecutions");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<OrderExecutions>("PModel.FK__orderExec__refer__70DDC3D8", "OrderExecutions", value);
}
}
}提前谢谢你。
发布于 2011-05-26 12:49:04
查询应该重写为
var orders = from order in context.Orders
select new
{
reference = order.reference,
OrderExecutions = order.OrderExecutions //this is an EntityCollection
};不需要转换为IEnumerable,然后重新选择相同的字段。然后不需要转换为List,然后返回到IQueryable,因为LINQ查询自然会返回一个IQueryable对象。通过调用ToList(),将对数据源执行查询。几乎总是更好的是推迟这些电话,直到任何额外的限制已经到位。
https://stackoverflow.com/questions/4546875
复制相似问题