I'm trying to get this left outer join working, but I seem to be encountering some issues. I have taken the example code from the MSDN left join article. This example is in LINQ syntax, but I want mine in extension method syntax, so I have also referenced this SO question.
Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
Pet barley = new Pet { Name = "Barley", Owner = terry };
Pet boots = new Pet { Name = "Boots", Owner = terry };
Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
// Create two lists.
List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };
var query = people
.GroupJoin(pets,
p1 => p1.FirstName,
p2 => p2.Owner.FirstName,
(p1, p2) => new {p1,p2})
.SelectMany(x => x.p2.DefaultIfEmpty(),
(x, y) => new { FirstName = x.p1.FirstName, PetName = y.Name });
foreach (var v in query)
{
Console.WriteLine("{0,-15}{1}", v.FirstName + ":", v.PetName);
}
My query is basically identical to the code I referenced, but I am getting this error:
NullReferenceException
Object reference not set to an instance of an object.
This should be extremely simple. What am I doing wrong?