
Originally Posted by
jmcilhinney
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:
var leftJoin = from s in students
join e in scores on s.StudentId equals e.StudentId into joinedScores
from jS in joinedScores.DefaultIfEmpty()
select new
{
score = jS ==null ? 0 : jS.Score,
testDate = jS == null ? "Did Not Take Test" : jS.TestDate.ToString("yyyy-dd-MM"),
testId = jS == null ? 0 :jS.TestId,
studentName = s.Name
};
var secondLeft = from t in exams join lj in leftJoin on t.TestId equals lj.testId into joinedTests
from jt in joinedTests.DefaultIfEmpty()
select new
{
testName = t.Name,
testId = t.TestId,
score = jt == null ? 0 : jt.score,
testDate = jt == null ? "Did not take test" : jt.testDate,
studentName = jt == null ? "Not available" : jt.studentName
};
foreach (var lJ in secondLeft)
{
Console.WriteLine("{3} \t{0}\t{1}\t{2}", lJ.score, lJ.studentName, lJ.testDate, lJ.testName);
}
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.