LINQ to SQL - 填充集合属性的子属性

时间:2012-11-28 03:32:14

标签: asp.net-mvc linq-to-sql ef-code-first

我在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; }
}

0 个答案:

没有答案