How do I convert this SQL inner join query into LINQ syntax?

旧时模样 提交于 2019-12-12 05:14:56

问题


I'm not that familiar with LINQ. I need this query converted into a LINQ statement for use inside my C# project.

Thanks

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

回答1:


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 };

Property Media will contain list of joined media entities. Also if you have navigation property defined in Gallery entity, then:

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


来源:https://stackoverflow.com/questions/14128287/how-do-i-convert-this-sql-inner-join-query-into-linq-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!