英文:
Join 2 tables and return the resulting one in LINQ
问题
db.Patients.Join(db.Studies, ct => ct.Studyid, st => st.Studyid, (ct, st) => st)
.Where(p => p.Patientstudyid == PatId)
.ToList()
英文:
I have 2 tables Patients
and Studies
that have a shared field called Studyid
. I need to get the study name from the Studies
table
where PatID
is a method's parameter.
I am joining the tables but I am failing to return the resulting one, so far I am retuning a single table to which I cannot implement the Where
condition.
db.Patients.Join(db.Studies, ct => ct.Studyid, st => st.Studyid, (ct, st) => st).Where(p => p.Patientstudyid == PatId).ToList()
which obviously won't work since Studies
have no patient's information.
How to return the joined table?
答案1
得分: 2
我更喜欢在使用连接时使用查询语法。这样更容易重构和理解查询的作用。
英文:
I would prefer query syntax when using joins. It is easily to refactor and to understand what query do.
var query =
from p in db.Patients
join st in db.Studies on p.Studyid equals ct.Studyid
where p.Patientstudyid == PatId
select st.Name;
var result = query.ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论