我试图让这个左外连接工作,但我似乎遇到了一些问题。我已经拿到了MSDN left join article的示例代码。这个例子是在LINQ语法中的,但是我希望我的扩展方法语法,所以我也引用了这个SO问题。左外连接错误
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);
}
我的查询基本上等同于我所引用的代码,但我得到这个错误:
NullReferenceException
Object reference not set to an instance of an object.
这应该是非常简单的。我究竟做错了什么?
查询语法更易于阅读。但是要回答你的问题,在你访问对象中的一个属性(从左连接)之前,你必须检查它是否为null。 (当使用Linq2Sql时,这是不需要的) – Magnus 2013-04-03 20:40:24