I am trying to write a linq to sql statement that will join a total of 3 tables. table 1 = users (userId) tabe 2 = userCourses (userId, CourseId), table 3 = courses(courseId).
This is what i am trying to do:
from u in db.Users join uc in userCourse on u.userId = uc.Id
join c in course on uc.courseId = c.courseId
where u.userId = uc.userId
select c.name
what is the proper syntax?
You are almost there, assuming your key types match. You just need to use the
equalskeyword in thejoinclauses:This is one of the few places where LINQ is slightly odd, since we cant use the equality operator in the join clause, but need to use a keyword not used anywhere else in the language. It also means that we can’t join on an arbitrary expression.