我正在学习MVC,它被证明是一个令人钦佩的敌人,看我是如何习惯网络形式的。
我可以在网络表单中做很多事情,我似乎无法在MVC中复制,如果我能在实际实现它时迷失方向。但我的问题如下;
我有一张桌子
public partial class Patient
{
public Patient()
{
this.PatientHousings = new HashSet<PatientHousing>();
}
public Guid ID {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public bool Incarcerated {get;set;}
public bool Released {get;set;}
public bool Deceased {get;set;}
public virtual ICollection<PatientHousing> PatientHousings {get;set;}
}
这是另一张表
public partial class PatientHousing
{
public Guid ID {get;set;}
public Guid PatientID {get;set;}
public bool CurrentPrevious {get;set;}
public string Building {get;set;}
public virtual Patient Patient {get;set;}
}
好的,所以在Web表单中我只需要使用正确的查询将下拉列表绑定到数据源。好吧,我无法用MVC做到这一点,而且我必须使用控制器,我仍然用头撞击墙壁。
我已经读过我需要创建一个ViewModel来帮助我这个,所以我创建了以下内容
public class PatientViewModel
{
public List<Patient> patients {get;set;}
public PatientViewModel(){
DATAEntities db = new DATAEntities();
patients = db.Patients.Include(r => r.PatientHousings.Where(h => h.CurrentPrevious == true)).Where(x => x.Incarcerated == false && x.Released == false && x.Deceased == false && !string.IsNullOrEmpty(x.LastName)).ToList();
}
}
现在我收到此错误
Include路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。
有人请帮忙。我现在迷路了,它甚至都不好笑。
澄清:我需要做的是,在视图中创建一个选择列表,列出所有未被监禁,释放或死亡的患者。
答案 0 :(得分:0)
我认为你不能使用&#39; where&#39;在Include中,因为它不是允许的导航属性之一。
https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx
Is there a way to defined .Where inside .Include
您可以尝试使用Select或没有它的表达式,然后使用Where进一步过滤掉。