flean
flean

Reputation: 27

Entity Framework Relationships Foreach

I have these classes and I'm using a Repository pattern and trying to get an IEnumerable to include the WorkOrderDetail. I'm new to MVC and Entity Framework and I can't figure out how to include the WorkOrderDetails.

public IEnumerable<Schedule> GetFuture()
        {
            string tZone = "Pacific Standard Time";

            DateTime today = Today(tZone);

            var schedule = context.Schedules
                .Where(s => s.Date >= today).ToList();            

            return schedule;
        }

public abstract class Entity
    {
        public int Id { get; set; }
    }

 public class Schedule : Entity
    {
        public DateTime Date { get; set; }

        public bool Free { get; set; }

        public int WorkOrderId { get; set; }
        public virtual WorkOrder WorkOrder { get; set; }          
    }

 public class WorkOrder : Entity
    {

        public string Name { get; set; }
        public string Type { get; set; }

        public virtual ICollection<WorkOrderDetail> WorkOrderDetails { get; set; }
     }
public class WorkOrderDetail : Entity
    {
       public string Job { get; set; }
       public bool Finished { get; set; }
    }

Upvotes: 0

Views: 694

Answers (1)

CodingGorilla
CodingGorilla

Reputation: 19852

Change this:

var schedule = context.Schedules.Where(s => s.Date >= today).ToList();    

to:

var schedule = context.Schedules.Include("WorkOrder.WorkOrderDetail").Where(s => s.Date >= today).ToList();

Upvotes: 1

Related Questions