将带有联接的SQL查询转换为lambda表达式



不确定如何将以下sql转换为lambda表达式。我的数据库在1对多关系中使用引用完整性和与表Content_Training相关的表Content(1个内容可以有多个Content_Training)

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

尝试此查询:

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

相关内容

  • 没有找到相关文章

最新更新