Friday, June 5, 2009

Left Outer Join in LINQ

Left Outer Join in LINQ
My example shows how to perform a left join
To do a left outer join, you need use SelectMany and DefaultIfEmpty.

DataContext objDb = new DataContext();
var objSectionInst = (from s in objDb.Sections
join sim in objDb.SectionInstructorMaps on s.SectionId equals sim.SectionId into temp
from t in temp.DefaultIfEmpty()
where s.CourseId == courseId
select new
{
SectionInstructorMapId = = t == null ? "0" : t.SectionInstructorMapId.ToString(),
UserId = = t == null ? "0" : t.UserId.ToString(),
SectionId = = t == null ? "0" : s.SectionId.ToString(),
SectionName = s.Name,
SectionTermName = s.Term.Name,
SectionTermYear = s.TermYear,
UserFullName = t.User.UserName
});

grdvInst.DataSource = objSectionInst;
grdvInst.DataBind();

No comments: