Внутреннее соединение LINQ против соединения слева

Вам нужно получить объединенные dotnet объекты в набор, а затем left-join применить DefaultIfEmpty, как language-integrated-query сказал JPunyon:

Person magnus = new Person { Name = "Hedlund, Magnus" };
Person terry = new Person { Name = "Adams, Terry" };
Person charlotte = new Person { Name = "Weiss, Charlotte" };

Pet barley = new Pet { Name = "Barley", Owner = terry };
List people = new List { magnus, terry, charlotte };
List pets = new List{barley};

var results =
    from person in people
    join pet in pets on person.Name equals pet.Owner.Name into ownedPets
    from ownedPet in ownedPets.DefaultIfEmpty(new Pet())
    orderby person.Name
    select new { OwnerName = person.Name, ownedPet.Name };


foreach (var item in results)
{
    Console.WriteLine(
        String.Format("{0,-25} has {1}", item.OwnerName, item.Name ) );
}

Выводы:

Adams, Terry              has Barley
Hedlund, Magnus           has
Weiss, Charlotte          has

c#

.net

linq

left-join

2022-05-29T22:44:57+00:00