gpt4 book ai didi

linq - 在 Linq 投影中设置 EntityCollection

转载 作者:行者123 更新时间:2023-12-05 01:12:20 40 4
gpt4 key购买 nike

我想知道如何在 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 涉及此错误:

The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph.

OrderExecutions 是对象 Orders 中包含的 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);
}
}
}

提前谢谢你。

最佳答案

查询应该重写为

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(),查询针对数据源执行。将这些电话推迟到任何其他限制实现后几乎总是更好。

关于linq - 在 Linq 投影中设置 EntityCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546875/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com