2

合計 3 つのテーブルを結合する linq to sql ステートメントを作成しようとしています。表 1 = ユーザー (userId)、表 2 = userCourses (userId、CourseId)、表 3 = コース (courseId)。

これは私がやろうとしていることです:

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

適切な構文は何ですか?

4

2 に答える 2

4

キーの種類が一致していれば、ほぼ完了です。句でequalsキーワードを使用するだけです。join

from u in db.Users join uc in userCourse on u.userId equals uc.Id
                   join c in course on uc.courseId equals c.courseId   
                   where u.userId = uc.userId
                   select c.name

これは、結合句で等値演算子を使用できず、言語の他の場所では使用されていないキーワードを使用する必要があるため、LINQ が少し奇妙である数少ない場所の 1 つです。また、任意の式で結合できないことも意味します。

于 2013-02-07T21:29:24.463 に答える
1

試す

from u in db.Users 
join uc in userCourse on u.userId equals  uc.Id
join c in course on uc.courseId equals c.courseId

where u.userId = uc.userId
select c.name

下記リンクも参照

https://web.archive.org/web/20101030154925/http://blogs.msdn.com/b/tikiwan/archive/2010/06/18/linq-to-sql-inner-join-left-join- examples-tutorial-samples-the-basic.aspx

于 2013-02-07T21:32:24.603 に答える