我讀左外連接在http://msdn.microsoft.com/en-us/library/vstudio/bb397895.aspx爲什麼linq join是單向的?
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
public static void LeftOuterJoinExample()
{
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 = from person in people
join pet in pets on person equals pet.Owner into gj
from subpet in gj.DefaultIfEmpty()
select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };
foreach (var v in query)
{
Console.WriteLine("{0,-15}{1}", v.FirstName + ":", v.PetName);
}
}
// This code produces the following output:
//
// Magnus: Daisy
// Terry: Barley
// Terry: Boots
// Terry: Blue Moon
// Charlotte: Whiskers
// Arlene:
我想了解外連接,所以我調整了一點點
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" };
**Person Momo = new Person { FirstName = "Momo", LastName = "Shawn" };**
**Pet kaw = new Pet { Name = "Kaw", Owner = Momo };**
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, **kaw** };
var query = from pet in pets
join person in people on pet.Owner equals person into gj
from subpet in gj.DefaultIfEmpty()
select new { name = (pet.Owner == null ? "unknown": person.FirstName) , PetName = (subpet == null ? String.Empty : subpet.Name) };
foreach (var v in query)
{
Console.WriteLine("{0,-15}{1}", v.name + ":", v.PetName);
}
智能感知不顯示人。爲什麼?我一直認爲你可以在一個連接中交換左側和右側。但顯然,你不能。
有沒有任何文件解釋這個?
我不明白從gj中的subpet是什麼意思。 gj應該是對象對。看起來subpet指的是加入右側的對象 –
@ user1978421:否,'gj'不是對象對 - 它是一組'Person'引用。目前還不清楚爲什麼你期望它是成對的。不要忘記,「寵物」仍然在範圍內...... –
我明白了。這就像gj是一個指針列表。但是,爲什麼人們自動刪除空指針(如果不使用DefaultIfEmpty()?) –