CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var list1 = new List<int>{1, 2, 3, 6, 4};
var list2 = new List<int>{4, 1, 2};
var selection =
from l1 in list1
join l2 in list2 on l1 equals l2 into joined
from j in joined.DefaultIfEmpty()select j;
var depts = new List<Department>{new Department{Name = "Accounting"}, new Department{Name = "IT"}, new Department{Name = "Marketing"}};
var persons = new List<Person>{new Person{DeptName = "Accounting", Name = "Bob"}};
var selection2 =
from d in depts
join p in persons on d.Name equals p.DeptName into joined2
from j2 in joined2.DefaultIfEmpty(new Person{DeptName = "Unknown", Name = "Alien"})select j2;
foreach(var thisJ in selection2)
{
Console.WriteLine("Dept: {0}, Name: {1}", thisJ.DeptName, thisJ.Name);
}
}
public class Person
{
public string Name
{
get;
set;
}
public string DeptName
{
get;
set;
}
}
public class Department
{
public string Name
{
get;
set;
}
}
}