How to select all records from one table that have corespondent keys with other, using joining table. I have table dbo.Patients and dbo.Hospitals and dbo.PatientHospitals which has Primary keys from both tables. I need all recods from dbo.Patients that are in dbo.Hospitals, that is that are in joining table dbo.PatientHospitals, something like:
var currentHospital = db.Hospitals.Single(x => x.User_Id == UserID);
var model = db.Patients.Include("Hospitals")
.Select(x => x.Hospitals == currentHospital);
In code first, you do not need additional table for simple many-to-many relationship. You could define your entities like this
And then you could just write
currentHospital.Patients