LINQ outer join


/ Published in: C#
Save to your folder(s)

NOTE: When you do an outer join, every column selected that could be null from the join must have a ternary operator in the select statement.


Copy this code and paste it in your HTML
  1. var allTeamMembers = from t in db.TeamMembers
  2. join ct in db.CaseTeamMembers on t.TeamMemberID equals ct.TeamMemberID into ctm
  3. from ct in ctm.DefaultIfEmpty()
  4. select new
  5. {
  6. CaseID = ct.CaseID == null ? 0 : ct.CaseID,
  7. t.TeamMemberID,
  8. CaseTeamMemberID = ct.CaseTeamMemberID == null ? 0 : ct.CaseTeamMemberID,
  9. t.Name,
  10. Photo = t.Photo ?? "default.gif"
  11. };

URL: http://www.hookedonlinq.com/OuterJoinSample.ashx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.