问题描述
对于这个我有3个型号:医院,AspNetUsers,UserHospitals
For this I have 3 models: Hospitals, AspNetUsers, UserHospitals.
这是UserHospitals模型:
this is the UserHospitals model:
public class UserHospital
{
[Key]
public int UserHospitalID { get; set; }
public int HospitalID { get; set; }
public Hospital Hospitals { get; set; }
public string Id { get; set; }
public ApplicationUser Users { get; set; }
}
有了它,我可以用户ID和医院ID添加到该表中。
With it I can add User ID and Hospital ID to this table.
现在,我需要检查哪些医院用户的连接。
Now, I need to check which hospitals user's connected.
在我的控制器,它返回一个列表的医院,我需要只返回用户有一个连接医院。
On my controller that return an hospital list i need to return only Hospitals that user's have a connection.
这回所有的医院,我怎么可以过滤以仅显示用户是否有UserHospitals与医院的连接?
This return all hospitals, how can I filter it to show only if user have a connection with hospital on UserHospitals?
public ActionResult Index()
{
return View(db.Hospitals.ToList());
}
我不希望添加新的视图模型的加入模型或任何
I don't want to add a new viewmodel that join models or whatever
== ==编辑
示范医院
public class Hospital
{
[Key]
public int HospitalID { get; set; }
public string Name { get; set; }
public virtual ICollection<HospitalSpeciality> HospitalSpecialities { get; set; }
public virtual ICollection<UserHospital> UserHospitals { get; set; }
}
推荐答案
试试这个:
public ActionResult Index()
{
var result =db.Hospitals.Include("UserHospitals").where(x=> x.UserHospitals.Any(x=>x.Id== userId)).ToList();
return View(result);
}
这篇关于配置多对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!