Quote Originally Posted by jmcilhinney View Post
You use the DefaultIfEmpty method on the right-hand list to perform a left outer join:

http://www.hookedonlinq.com/OuterJoinSample.ashx
That would work very well with two tables, but with three lists, I run into a bit of a problem.

Here's my code to join the three lists.

C# Code:
  1. var leftJoin = from s in students
  2.                            join e in scores on s.StudentId equals e.StudentId into joinedScores
  3.                            from jS in joinedScores.DefaultIfEmpty()
  4.                                select new
  5.                                    {
  6.                                      score = jS ==null ? 0 :  jS.Score,
  7.                                      testDate = jS == null ? "Did Not Take Test" : jS.TestDate.ToString("yyyy-dd-MM"),
  8.                                      testId = jS == null ? 0 :jS.TestId,
  9.                                      studentName = s.Name
  10.                                    };
  11.  
  12.             var secondLeft = from t in exams join lj in leftJoin on t.TestId equals lj.testId into joinedTests
  13.             from jt in joinedTests.DefaultIfEmpty()
  14.                         select new
  15.                                 {
  16.                                     testName = t.Name,
  17.                                     testId = t.TestId,
  18.                                     score = jt == null ? 0 : jt.score,
  19.                                     testDate = jt == null ? "Did not take test" : jt.testDate,
  20.                                     studentName = jt == null ? "Not available" : jt.studentName
  21.                                 };
  22.  
  23.            
  24.             foreach (var lJ in secondLeft)
  25.             {
  26.                 Console.WriteLine("{3} \t{0}\t{1}\t{2}", lJ.score, lJ.studentName, lJ.testDate, lJ.testName);
  27.             }

here is the output it produces
PHP Code:
English         9.11    Steyn Jacob     2013-01-01
English         9.11    Elisha Martin   2013
-01-01
English         9.11    Jim Martin      2013
-01-01
Math    8.11    Steyn Jacob     2013
-01-02
Science         8.11    Elisha Martin   2013
-01-03
Geography       0       Not available   Did not take test
French  0       Not available   Did not take test
History         0       Not available   Did not take test 
I would instead prefer to have a listing that has all the students as well as all the tests.