0

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.....
}
3

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.