如何使用Lambda查询语法在LINQ上进行左联接

时间:2018-08-10 16:25:49

标签: c# linq

在我的示例中,我要在此处执行的操作是使用带有Lambda查询语法的LINQ进行左联接。但是我总是得到内部联接的结果。这是我的示例数据:

        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" };

这是我在清单上的声明:

List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };

现在这是我想要的LINQ查询,它是一个左联接:

var query1 = people.Join(pets, oP => oP.FirstName.ToLower(), oPe => oPe.Owner.ToLower(), 
            (peeps, peets)
            => new 
            {
                PersonFirstName = peeps.FirstName,
                PersonLastName = peeps.LastName,
                DogName = peets?.Name ?? string.Empty
            });

但是我收到的是内部联接结果:

enter image description here

我想念

  

阿琳

应该是结果的一部分。我认为我的LINQ查询中缺少某些内容。

0 个答案:

没有答案