如何使用 LINQ to Entities(带有转换)返回嵌套数据集?
我正在将实体对象转换为我自己的模型(CustomerModel 和 OrderModel)。
我目前有一个返回我的客户的 IQueryable 方法:
IQueryable<CustomerModel> GetCustomers()
{
return from c in entities.Customers
select new CustomerModel {
CustomerId = c.CustomerId,
CustomerName = c.CustomerName
// Orders = ??
};
}
在我的 CustomerModel 中,您可以看到我有一个订单数组。
class CustomerModel {
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public OrderModel[] Orders { get; set; }
}
class OrderModel {
public int OrderId { get; set; }
public int CustomerId { get; set; }
public string OrderDetails { get; set; }
}
当订单类型为数组时,我似乎无法弄清楚如何在客户对象内部设置订单。
当我将 CustomerModel 的 Orders 更改为 IEnumerable 而不是数组时,我能够做到这一点,并且您不能将 .ToArray() 与 LINQ to Entities 一起使用,所以我陷入了困境。
所以我的问题是:使用订单数组可以做到这一点吗?
问题解答:
- 为什么是数组?我在其他地方返回一个数组(供其他项目使用),并且让 Orders 也成为一个数组是有意义的。
此外,您不能对订单使用 ToArray(),会引发此错误:
System.NotSupportedException: LINQ to Entities does not recognize the method 'Proj.Models.Orders[] ToArray[Orders](System.Collections.Generic.IEnumerable`1[Proj.Models.Orders])' method, and this method cannot be translated into a store expression.
I'm performing a transform on the entity objects into my own models (CustomerModel and OrderModel).
I currently have an IQueryable method that returns my Customers:
IQueryable<CustomerModel> GetCustomers()
{
return from c in entities.Customers
select new CustomerModel {
CustomerId = c.CustomerId,
CustomerName = c.CustomerName
// Orders = ??
};
}
In my CustomerModel, you can see I have an array of Orders.
class CustomerModel {
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public OrderModel[] Orders { get; set; }
}
class OrderModel {
public int OrderId { get; set; }
public int CustomerId { get; set; }
public string OrderDetails { get; set; }
}
I can't seem to figure out how set Orders inside of the Customer object when the Orders type is Array.
I was able to do it when I changed the CustomerModel's Orders to be IEnumerable instead of an array and you cannot use .ToArray() with LINQ to Entities, so I'm stuck.
So my question is: is this possible to do using an Array for Orders?
Answers to Questions:
- Why an Array? I'm returning an array (for consumption by other projects) elsewhere and it made sense to have the Orders be an Array as well.
Also, you cannot use ToArray() for the Orders, this error is thrown:
System.NotSupportedException: LINQ to Entities does not recognize the method 'Proj.Models.Orders[] ToArray[Orders](System.Collections.Generic.IEnumerable`1[Proj.Models.Orders])' method, and this method cannot be translated into a store expression.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
Orders
可以从Customers
访问,您可以尝试:这需要您将
Customer.Orders
更改为IEnumerable。
。这是在对数据库的一次请求中加载对象图的唯一方法。If your
Orders
are accessible fromCustomers
, you can try:This requires you to change
Customer.Orders
to anIEnumerable<Order>
. That is the only way to load the object graph in one request to the database.当我们的数据访问方法返回业务对象(模型)时,这不是很好的做法。您可以通过以下方式编辑您的查询:
如果此解决方案对您没有用,请描述您的“实体”
Not very good practise when our Data Access method returns business objects (models). You can edit your queries for example in such way:
if this solution is not useful for you, please describe your "entities"