sql - C# Left join in LINQ -


this question has answer here:

not sure how convert following sql linq expression. db use referential integrity , table content related table content_training in 1 many relationship (ex: 1 content can have many content_trainings).

select c.contentid, c.name, ct.trainingtypeid  dbo.content c left join dbo.contenttraining ct on c.contentid = ct.contentid c.expirationdate not null order ct.trainingtypeid, c.name 

i have tried this, seems work. however, not usage of "let" keyword.

var data = (from c in context.contents let ct = ( t in context.content_training t.contentid == c.contentid select new { t.trainingtypeid } ).firstordefault() c.expirationdate.hasvalue orderby ct.trainingtypeid, c.name select new { c.contentid, c.name, ct.trainingtypeid } ).tolist(); 

for left join, need use defaultifempty()

your query should similar this:

var query = c in content             join ct in contenttraining             on c.contentid equals ct.contentid g             ct in g.defaultifempty()             c.expirationdate != null             select new             {                      c.contentid,                  c.name,                  ct.trainingtypeid               }).tolist(); 

please refer left outer join in linq

and

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


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -