我需要帮助将SQL查询重写为LINQ格式

时间:2013-01-04 21:29:39

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

我对LINQ还不是很好,可以在语法上使用一些帮助。

谢谢!

以下查询需要在LINQ for C#中编写。

SELECT Galleries.GalleryTitle, Media.*
FROM Galleries 
INNER JOIN Media ON Galleries.GalleryID = Media.GalleryID
WHERE (Galleries.GalleryID = 150)
ORDER BY MediaDate DESC, MediaID DESC

2 个答案:

答案 0 :(得分:1)

这样的事情:

var query = db.Galleries
            .Join(db.Media, g => g.GalleryID, m => m.GalleryID, (g, m) => new {g, m})
            .Where(r.g.GalleryID == 150)
            .Select(res => new {res.g.GalleryTitle, Media = res.m}
            .OrderByDescending(o => o.Media.MediaDate)
            .ThenByDescending(o => o.Media.MediaID);

答案 1 :(得分:1)

或使用查询语法:

var query = from g in db.Galleries
            join m in db.Media on g.GalleryID equals m.GalleryID
            where g.GalleryID == 150
            orderby m.MediaDate descending, m.MediaID descending
            select new { g.GalleryTitle, Media = m };