Entity Framework Include Nest collection and nested properties

c# entity-framework entity-framework-core

Question

I have 4 classes

public class Customer
{
    public string CustomerName { get; set; }
    public ICollection<Order> Orders{ get; set; }
}

public class Order
{
    public string OrderNumber { get; set; }
    public ICollection<OrderLine> OrderLines { get; set; }
    public OrderType Type { get; set; }
}

public class OrderLine
{
    public string StockItem { get; set; }
}

public class OrderType
{
    public string Type { get; set; }
}

Using entity frameworks i want to pull out all the information starting at Customer level. I can get most information out but i am stuck getting OrderType to show against the order.

This is what i have so far.

var customerOrderDetails = _myOrderRepository
                           .GetAll()
                           .Include(o => o.Orders)
                           .ThenInclude(l => l.OrderLines)

I've tried added select after the o.Orders but that doesn't seem to work.

1
0
11/9/2017 12:41:55 PM

Accepted Answer

You can (and should) repeat the Include(x => x.ProjectActivityTasks) part:

var qry = await _projectActivityRepository.GetAll()
.Include(x => x.ProjectActivityVehicles)
  .ThenInclude(x => x.Vehicle)
.Include(x => x.ProjectActivityTasks)
  .ThenInclude(x => x.ProjectActivityTaskType)
.Include(x => x.ProjectActivityTasks)
  .ThenInclude(x => x.UnitOfMeasure)
.Where(x => x.Id == Id && x.TenantId == (int)AbpSession.TenantId)
.FirstOrDefaultAsync();
7
9/30/2017 8:42:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow