如何将此SQL内部联接查询转换为LINQ语法?

时间:2013-01-02 19:54:24

标签: sql-server linq entity-framework sql-to-linq-conversion

我对LINQ并不熟悉。我需要将此查询转换为LINQ语句,以便在我的C#项目中使用。

由于

SELECT Galleries.GalleryTitle, Media.*
FROM Galleries 
INNER JOIN Media ON Galleries.GalleryID = Media.GalleryID
WHERE (Galleries.GalleryID = 100)

1 个答案:

答案 0 :(得分:4)

var query = from g in db.Galleries
            join m in db.Media on g.GalleryID equals m.GalleryID into gm
            where g.GalleryID == 100
            select new { g.GalleryTitle, Media = gm };

属性Media将包含已加入媒体实体的列表。此外,如果您在Gallery实体中定义了导航属性,那么:

var gallery = db.Galleries.Include("Media")
                .FirstOrDefault(g => g.GalleryID == 100);