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.
If your
Ordersare accessible fromCustomers, you can try:This requires you to change
Customer.Ordersto anIEnumerable<Order>. That is the only way to load the object graph in one request to the database.