Reputation: 13479
Please consider this code:
var res = (from a in ent.Orders
where a.OrderID < 10252
select new
{
OrderId=a.OrderID,
OrderDate = a.OrderDate,
OrderDetails=ent.Order_Details.Where(o=>o.OrderID == a.OrderID).ToList()
}).ToList() ;
When I ran this code I got this error:
Additional information: LINQ to Entities does not recognize the method 'System.Collections.Generic.List
1[NorthwindModel1.Order_Detail] ToList[Order_Detail](System.Collections.Generic.IEnumerable
1[NorthwindModel1.Order_Detail])' method, and this method cannot be translated into a store expression.
I want to get Orders
with a list of their details.
How I can do this with one linq statement?
thanks
Upvotes: 0
Views: 44
Reputation: 11741
Well you can make a model class as shown below :
public class ViewModelClass
{
public int OrderId{ get; set; }
public int OrderDate { get; set; }
public List<NorthwindModel1.Order_Detail> OrderDetails{ get; set; }
}
and then in your query :-
var res = (from a in ent.Orders
where a.OrderID < 10252
select new ViewModelClass
{
OrderId=a.OrderID,
OrderDate = a.OrderDate,
OrderDetails=ent.Order_Details.Where(o=>o.OrderID == a.OrderID).ToList()
}).ToList();
Upvotes: 0
Reputation: 334
If you are using EF you can .Include("OrderDetails") when you request your Orders. Though I admit I dont remember if northwind database has the relationships set.
Something like:
var dbctx= new NorthwindContext();
var orders= dbctx.Orders.Include("OrderDetails").Where(o=>o.OrderID<10252).ToList();
Upvotes: 0