我在 ASP.NET MVC4 项目中使用 Code First 和 LINQ to SQL。在下面的查询中,我试图填充 PatientView.Appointments.ScheduledBy,但它返回 null。我尝试添加 .Include("Appointments.ScheduledBy"),但 Appointments.ScheduledBy 继续返回 null。
如何修改 LINQ to SQL 表达式以填充 ScheduledBy?
这是我的 LINQ to SQL(Id 是操作的参数)
var q = from p in context.Patients.Include("Appointments.ScheduledBy")
where p.Id == Id
select new PatientView
{
Patient = p,
Appointments = p.Appointments.OrderByDescending(a => a.ScheduledFor)
};
PatientView pv = q.Single();
PatientView 是视图的视图模型。Appointments 属性确实被填充,但 Appointments 的 ScheduledBy 属性为空。
public class PatientView
{
public Patient Patient { get; set; }
public IEnumerable<Appointment> Appointments { get; set; }
}
public class Patient
{
public int Id { get; set; }
public string Number { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Appointment> Appointments { get; set; }
}
public class Appointment
{
public int Id { get; set; }
public Patient Patient { get; set; }
public Employee ScheduledBy { get; set; }
public DateTime ScheduledFor { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}