3

不确定如何将以下 sql 转换为 lambda 表达式。我的数据库以一对多的关系使用与表 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
4

1 回答 1

2

试试这个查询:

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);
于 2014-04-09T03:11:04.360 回答