[英]LINQ query from ViewModel to Controller ViewBag to display in SelectList in View
I am in the process of learning MVC, and it is proving to be quite an admirable foe seeing as how I'm used to web forms.我正在学习 MVC,事实证明这是一个非常令人钦佩的敌人,因为我已经习惯了 Web 表单。
There are so many things I can do in web forms that I can't seem to replicate in MVC, and if I can I'm so lost when it comes to actually implementing it.我可以在 Web 表单中做很多事情,我似乎无法在 MVC 中复制,如果可以的话,我在实际实现它时会很迷茫。 But my question is as follow;
但我的问题如下;
I have a table我有一张桌子
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;}
}
And this is a other table这是另一张桌子
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;}
}
Okay, so in web forms I would just bind a dropdownlist to a datasource with the proper query.好的,所以在 Web 表单中,我只需将下拉列表绑定到具有正确查询的数据源。 Well I can't do that with MVC, and I have to use the controller which I'm still bashing my head against the wall with.
好吧,我不能用 MVC 做到这一点,而且我必须使用控制器,我仍然用它来撞墙。
I've read that I need to create a ViewModel to help me with this, so I've created the following我读过我需要创建一个 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();
}
}
Now I'm getting this error现在我收到这个错误
The Include path expression must refer to a navigation property defined on the type.
Include 路径表达式必须引用类型上定义的导航属性。 Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
对参考导航属性使用虚线路径,对集合导航属性使用 Select 运算符。
Someone please help.有人请帮忙。 I'm so lost right now it isn't even funny.
我现在很失落,这甚至都不好笑。
To clarify: What I need to do is, create a select list in a view that lists all patients that are not incarcerated, released, or deceased.澄清一下:我需要做的是,在一个视图中创建一个选择列表,其中列出了所有未被监禁、释放或死亡的患者。
I think you cannot use a 'where' inside Include as it isn't one of allowed navigation properties.我认为您不能在 Include 中使用“where”,因为它不是允许的导航属性之一。
https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx https://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx
Is there a way to defined .Where inside .Include 有没有办法定义 .Where 里面 .Include
You can try using Select or an expression without it and then filter out further using Where.您可以尝试使用 Select 或没有它的表达式,然后使用 Where 进一步过滤掉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.