3

Not sure how to convert the following sql into a LINQ expression. My db does use referential integrity and table Content is related to table Content_Training in a 1 to many relationship (ex: 1 content can have many content_trainings).

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

I have tried this, which seems to work. However, I am not certain about the usage of the "let" keyword.

var data = (from c in context.Contents
let ct = ( from t in context.Content_Training where t.ContentId == c.ContentId
select new { t.TrainingTypeId } ).FirstOrDefault()
where c.ExpirationDate.HasValue
orderby ct.TrainingTypeId, c.Name
select new { c.ContentId, c.Name, ct.TrainingTypeId } ).ToList();
4
  • @the 8th bit: Added what I have tried so far. Commented Apr 9, 2014 at 2:42
  • Your title says "left join" but the SQL you show is doing an inner join, so I'm unclear as to which you're trying to accomplish. Commented Apr 9, 2014 at 2:54
  • Craig W: Sorry about that. Yes, I do want a left join. I edited my sql. Commented Apr 9, 2014 at 3:06
  • did you just ask the exact same question under another username? stackoverflow.com/questions/22951644/… Commented Apr 9, 2014 at 3:17

2 Answers 2

20

For left join, you need to use DefaultIfEmpty()

Your query should be something similar to this:

var query = from c in Content
            join ct in ContentTraining
            on c.ContentId equals ct.ContentId into g
            from ct in g.DefaultIfEmpty()
            where c.ExpirationDate != null
            select new
            {     
                c.ContentId, 
                c.Name, 
                ct.TrainingTypeId 
             }).ToList();

Please refer to Left outer join in linq

and

http://msdn.microsoft.com/en-us/library/bb397895.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Rashmi! Regarding Craig W's response below, does "include" do a left join?
I'm not sure of "Include". Maybe Craig can answer. Did my solution work? I couldn't test it as I don't have VS.Net right now.
For left join in sql try this stackoverflow.com/a/75658735/7952953
2

Alternatively, you might consider using lambda instead of expression syntax. I haven't tried this but this should get you what you need (if what you need is a left join).

var foo = context.Contents.Include( "Content_Training" )
    .Where( c => c.ExpirationDate != null )
    .OrderBy( c => c.Content_Training.TrainingTypeId )
    .ThenBy( c => c.Name
    .Select( c => new { c.ContentId, c.Name, c.Content_Training.TrainingTypeId } );

3 Comments

So "include" does a left join?
Also, in this case I am interested in the LINQ version. I am unsure about my use of the "let" keyword and wanted some info and opinions on that.
It's not a left join per se but the results are essentially the same, if there are no Content_Training objects associated with the Content object you will still get the Content object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.