I'm going to simplify a little bit my model in order to show what I want to do. I know perfectly how do that using SQLbut I'm having problems with EF and LINQ.
Sorry for the long post.
I have the next POCO's:
public class Order
{
public int Id {get; set;}
public double Quantity {get; set;}
public List<Status> StatusChanges{get; set;}
}
public class Status
{
public int Id {get; set;}
public DateTime StatusDate {get; set;}
public StatusType StatusType {get; set;}
}
public class StatusType
{
public int Id {get; set;}
public string Code {get; set;}
public string Description {get; get;}
}
An Order
have multiple Status
and one Status
have only one StatusType
My questions:
1) How to select in a LINQ query a list containg all the orders with the loaded StatusType
I try with
var results= (from o in db.Orders
select new {o.Id,o.Quantity,o.StatusChanges})
.Include(o=>o.StatusChanges.Select(s=>s.StatusType));
But that's not that I want, I want a list which contains, Order
, Status
and StatusType
(even if the Order
info is repetead) I know I can make a double foreach but I would like to know if is a single query way (or a double query one to the db and another using linq to entities)
2) How make a list which contains the Order
Id and the Codes of all the status concatenated. Example
Orders:
Id Quantity
1 2
Status
Id StatusDate StatusTypeId
1 01-10-2014 1
2 02-10-2014 2
StatusType
Id Code Description
1 E Entered
2 C Confirmed
3 D Deleted
Result:
OrderId Quantity StatusResume
1 2 E,C
I know I can use this question but I don't know how have access to the loaded elements in the list.