There are tables Subject
, Student
and SubjectEnrolled
Subject
table have two columns SubjectId
and SubjectName
SubjectEnrolled
table also have two column StudentID
(foreign key to StudentTable
) and SubjectId
(foreign key to Subject
) I want to convert this SQL query
SELECT SubjectName
FROM Subject
WHERE SubjectId IN
(
SELECT SubjectId
FROM SubjectEnrolled
WHERE StudentID=7
)
Into a Linq or Lamda expression
using (var db = new DbContext())
{
var res = from r in db.Subjects.....
}
1 - SQL: use inner join
instead IN
:
SELECT SubjectName FROM Subject sub
INNER JOIN SubjectEnrolled subEn on sub.SubjectId = subEn.SubjectId
WHERE subEn.StudentID = 7
2 - Linq Query Join
:
var res = (from sub in db.Subjects
join subEn in db.SubjectEnrolleds on sub.SubjectId equals subEn.SubjectId
where subEn.StudentID = 7).ToList();
I hope you find this helpful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.