我对SQL有此限制,我只想用连接药物返回一种疾病。但是,这样做是在使所有疾病都退回并且仅填充符合where条件的药物。
我需要先选择药物然后再选择疾病才能起作用吗?
public List<Model.Disease> GetDiseaseById(Guid DiseaseId)
{
var Disease = (from Diseases in db.Diseases
join dr in db.DrugDiseases on equals d.DrugId on dr.DrugId
join rdDiseases on rdDiseases.Diseases equals rd.DiseaseId on dr.DiseaseId
where (aDrug.DiseaseId == DiseaseId)
select new Model.Disease
{
DiseaseId = rdDiseases.DiseaseId,
DiseaseName = rdDiseases.DiseaseName,
drug = (from d in db.Drug
join dr1 in db.DrugDiseases on equals d.DrugId on dr1.DrugId
join rd1Diseases on rdDiseases.Diseases equals rd.DiseaseId on dr1.DiseaseId
where (Diseases.DiseaseId == d.DiseaseId && d.DiseaseId == DiseaseId)
select new drug
{
DrugId = d.drug,
DrugName = d.drugName}).OrderBy(m => m.DrugName).ToList(),
}).OrderBy(d => d.DiseaseName).ToList().FirstOrDefault();
return Disease;
}
答案 0 :(得分:0)
基于上面的评论,您只需一个联接,这应该很简单。您应该大致了解Joins in LINQ和LINQ。另外,由于您只在寻找一种疾病,所以为什么要返回清单?下面的代码应该可以工作:
public Model.Disease GetDiseaseById(Guid DiseaseId)
{
return (from Disease in db.Diseases
where Disease.DiseaseId == DiseaseId
orderby Disease.DiseaseName
join d in db.Drug on Disease.DiseaseId equals d.DiseasesId
select new Model.Disease
{
DiseaseId = Disease.DiseaseId
DiseaseName = Disease.DiseaseName
drug = new { DrugId = d.DrugId, DrugName = d.DrugName }
}).FirstOrDefault();
}