简体   繁体   English

左外连接错误

[英]Left outer join errors

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 . 我从MSDN左联接文章中获取了示例代码。 This example is in LINQ syntax, but I want mine in extension method syntax, so I have also referenced this SO question. 此示例使用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);
        }

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? 我究竟做错了什么?

You're missing that part of sample query: 您缺少示例查询的那一部分:

PetName = (subpet == null ? String.Empty : subpet.Name)

On your query it's just PetName = y.Name , so whenever there is no corresponding row in pets list you're getting NullReferrenceException because y is null . 在您的查询中,它只是PetName = y.Name ,因此,只要pets列表中没有对应的行,您都会得到NullReferrenceException因为ynull

Should be: 应该:

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 == null ? String.Empty : y.Name) });

Or you can use DefaultIdEmpty(TSource) method overload: 或者,您可以使用DefaultIdEmpty(TSource)方法重载:

var def = new Pet { Name = string.Empty };

var query = people
            .GroupJoin(pets,
            p1 => p1.FirstName,
            p2 => p2.Owner.FirstName,
            (p1, p2) => new { p1, p2 })
            .SelectMany(x => x.p2.DefaultIfEmpty(def),
                (x, y) => new { FirstName = x.p1.FirstName, PetName = y.Name });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM