Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42326

3.0/LINQ How do I get a left outer join in LINQ.

$
0
0
Here is my code.

C# Code:
  1. namespace LINQMatching
  2. {
  3.     class Student
  4.     {
  5.         public int StudentId { get; set; }
  6.         public string Name { get; set; }
  7.         public DateTime DateOfBirth { get; set; }
  8.     }
  9.    
  10.     class Exam
  11.     {
  12.         public int TestId { get; set; }
  13.         public string Name { get; set; }
  14.     }
  15.  
  16.     class ExamScore
  17.     {
  18.         public int StudentId { get; set; }
  19.         public int TestId { get; set; }
  20.         public DateTime TestDate { get; set; }
  21.         public decimal ? Score { get; set; }
  22.     }
  23.  
  24.     class StudentScore
  25.     {
  26.         public int StudentId { get; set; }
  27.         public string StudentName { get; set; }
  28.         public int TestId { get; set; }
  29.         public string TestName { get; set; }
  30.         public decimal? TestScore { get; set; }
  31.         public DateTime TestDate { get; set; }
  32.     }
  33.  
  34.     class LinqMatching
  35.     {
  36.        static List<Student> students = new List<Student>();
  37.        static List<Exam> exams = new List<Exam>();
  38.        static List<ExamScore> scores = new List<ExamScore>();
  39.  
  40.         static void PopulateLists()
  41.         {
  42.             students = new List<Student>
  43.                 {
  44.                     new Student {StudentId = 101, Name = "Steyn Jacob", DateOfBirth = new DateTime(1987, 1, 1)},
  45.                     new Student {StudentId = 102, Name = "Elisha Martin", DateOfBirth = new DateTime(1989, 1, 1)},
  46.                     new Student {StudentId = 103, Name = "Chad Gomes", DateOfBirth = new DateTime(1989, 11, 21)},
  47.                     new Student {StudentId = 104, Name = "Jim Martin", DateOfBirth = new DateTime(1989, 12, 1)}
  48.                 };
  49.             exams = new List<Exam>
  50.                 {
  51.                     new Exam {TestId = 10, Name = "English"},
  52.                     new Exam {TestId = 11, Name = "Math"},
  53.                     new Exam {TestId = 13, Name = "Science"},
  54.                     new Exam {TestId = 14, Name = "Geography"},
  55.                     new Exam {TestId = 16, Name = "French"},
  56.                     new Exam {TestId = 15, Name = "History"}
  57.                 };
  58.             scores = new List<ExamScore>
  59.                 {
  60.                     new ExamScore {TestId = 10, Score = 9.11M, StudentId = 101, TestDate = new DateTime(2013, 1, 1)},
  61.                     new ExamScore {TestId = 11, Score = 8.11M, StudentId = 101, TestDate = new DateTime(2013, 2, 1)},
  62.                     new ExamScore {TestId = 13, Score = 8.11M, StudentId = 101, TestDate = new DateTime(2013, 3, 1)},
  63.                     new ExamScore {TestId = 10, Score = 9.11M, StudentId = 102, TestDate = new DateTime(2013, 1, 1)},
  64.                     new ExamScore {TestId = 10, Score = 9.11M, StudentId = 104, TestDate = new DateTime(2013, 1, 1)}
  65.                 };
  66.         }
  67.  
  68.         static void Main(string[] args)
  69.         {
  70.             var queryOut = from s in students
  71.                            join e in scores on s.StudentId equals e.StudentId
  72.                            join t in exams on e.TestId equals t.TestId
  73.                            select new StudentScore
  74.                                {
  75.                                    StudentName = s.Name,
  76.                                    TestId = e.TestId,
  77.                                    TestName = t.Name,
  78.                                    TestDate = e.TestDate,
  79.                                    StudentId = s.StudentId,
  80.                                    TestScore = e.Score
  81.                                };
  82.             foreach (var s in queryOut)
  83.             {
  84.                 Console.WriteLine("{0} {1} {2} {3} {4}",s.StudentId, s.StudentName, s.TestName, s.TestDate, s.TestScore);
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90. i wish to know the syntax for getting this join to work. The output I expect from the query is
  91. StudentName - TestName - TestDate - TestScore
  92.  
  93. The list must contain all the students and all the tests. If a student hasn't taken a test, the testscore and testdate would be empty.
  94. How can I form this query?

Viewing all articles
Browse latest Browse all 42326


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>