Convert SQL query with join to lambda expression

戏子无情 提交于 2019-12-02 05:31:57

问题


not sure how to convert the following sql into a lambda expression. My database uses referential integrity and table Content related to table Content_Training in a 1 to many relationship (1 content can have many content_trainings)

select c.ContentId, c.Name, ct.TrainingTypeId 
from dbo.Content c left join dbo.Content_Training ct on c.ContentId = ct.ContentId
where c.PublishDate is not null
order by ct.TrainingTypeId, c.Name

回答1:


Try this query:

var results = (from c in dbcontext.Contents
               join ct in dbcontext.Content_Trainings on c.ContentId equals ct.ContentId into t
               from rt in t.DefaultIfEmpty()
               select new
               {
                   c.ContentId,
                   c.Name,
                   TrainingTypeId = (int?)rt.TrainingTypeId
               }).OrderBy(r => r.TrainingTypeId)
                 .ThenBy(r => r.Name);


来源:https://stackoverflow.com/questions/22951644/convert-sql-query-with-join-to-lambda-expression

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