CSharp - Perform a left outer join using both the GroupJoin and DefaultIfEmpty operators.

Description

Perform a left outer join using both the GroupJoin and DefaultIfEmpty operators.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from w w w.j  a v a2 s . c  o m*/
{
    static void Main(string[] args)
    {
        ArrayList StudentsAL = Student.GetStudentsArrayList();
        //  Add a new Student so one Student will have no StudentOptionEntry records.
        StudentsAL.Add(new Student
        {
            id = 102,
            firstName = "Michael",
            lastName = "Bolton"
        });
        Student[] Students = StudentsAL.Cast<Student>().ToArray();
        StudentOptionEntry[] empOptions = StudentOptionEntry.GetStudentOptionEntries();

        var StudentOptions = Students
          .GroupJoin(
            empOptions,
            e => e.id,
            o => o.id,
            (e, os) => os
              .Select(o => new
              {
                  id = e.id,
                  name = string.Format("{0} {1}", e.firstName, e.lastName),
                  options = o != null ? o.optionsCount : 0
              }))
          .SelectMany(r => r);

        foreach (var item in StudentOptions)
            Console.WriteLine(item);


        //An Example with the DefaultIfEmpty Operator
        StudentsAL = Student.GetStudentsArrayList();
        //  Add a new Student so one Student will have no StudentOptionEntry records.
        StudentsAL.Add(new Student
        {
            id = 102,
            firstName = "Michael",
            lastName = "Bolton"
        });
        Students = StudentsAL.Cast<Student>().ToArray();
        empOptions = StudentOptionEntry.GetStudentOptionEntries();

        StudentOptions = Students
         .GroupJoin(
           empOptions,
           e => e.id,
           o => o.id,
           (e, os) => os
             .DefaultIfEmpty()
             .Select(o => new
             {
                 id = e.id,
                 name = string.Format("{0} {1}", e.firstName, e.lastName),
                 options = o != null ? o.optionsCount : 0
             }))
         .SelectMany(r => r);

        foreach (var item in StudentOptions)
            Console.WriteLine(item);
    }
}
class Student
{
    public int id;
    public string firstName;
    public string lastName;

    public static ArrayList GetStudentsArrayList()
    {
        ArrayList al = new ArrayList();

        al.Add(new Student { id = 1, firstName = "Joe", lastName = "Ruby" });
        al.Add(new Student { id = 2, firstName = "Windows", lastName = "Python" });
        al.Add(new Student { id = 3, firstName = "Application", lastName = "HTML" });
        al.Add(new Student { id = 4, firstName = "David", lastName = "Visual" });
        al.Add(new Student { id = 101, firstName = "Kotlin", lastName = "Fortran" });
        return (al);
    }

    public static Student[] GetStudentsArray()
    {
        return ((Student[])GetStudentsArrayList().ToArray());
    }
}

class StudentOptionEntry
{
    public int id;
    public long optionsCount;
    public DateTime dateAwarded;

    public static StudentOptionEntry[] GetStudentOptionEntries()
    {
        StudentOptionEntry[] empOptions = new StudentOptionEntry[] {
      new StudentOptionEntry {
        id = 1,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1990/12/31") },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 10000,
        dateAwarded = DateTime.Parse("1992/06/30")  },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 10000,
        dateAwarded = DateTime.Parse("1991/01/01")  },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 5000,
        dateAwarded = DateTime.Parse("1997/09/30") },
      new StudentOptionEntry {
        id = 2,
        optionsCount = 10000,
        dateAwarded = DateTime.Parse("2000/04/01")  },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 7500,
        dateAwarded = DateTime.Parse("1998/09/30") },
      new StudentOptionEntry {
        id = 3,
        optionsCount = 7500,
        dateAwarded = DateTime.Parse("1998/09/30") },
      new StudentOptionEntry {
        id = 4,
        optionsCount = 1500,
        dateAwarded = DateTime.Parse("1997/12/31") },
      new StudentOptionEntry {
        id = 101,
        optionsCount = 2,
        dateAwarded = DateTime.Parse("1998/12/31") }
    };

        return (empOptions);
    }
}

Result

Related Topic