3

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
BeYourOwnGod
  • 2,345
  • 7
  • 30
  • 35

1 Answers1

3

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);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
  • I'd use query syntax constantly, without lambdas, i.e. `orderby x,y`. It'll be more readable. – abatishchev Apr 09 '14 at 03:24
  • Thanks Abatishchev. In the past I have used the "let" keyword instead of the "into t ... t.DefaultIfEmpty()" that you used. What is the difference? – BeYourOwnGod Apr 09 '14 at 04:57