使用擴展語法我試圖在兩個使用LINQ的列表上創建一個左連接。以下是來自Microsoft的幫助,但我已將其修改爲顯示寵物列表中沒有元素。我最終得到的是0個元素的列表。我認爲這是因爲內部連接正在發生。我想結束的是3個元素(3個Person對象)的列表,其中填充缺失元素的空數據。即左連接。這可能嗎?LINQ Inner-Join vs Left-Join
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 };
//Pet boots = new Pet { Name = "Boots", Owner = terry };
//Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
//Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
List<Person> people = new List<Person> { magnus, terry, charlotte };
//List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };
List<Pet> pets = new List<Pet>();
// Create a list of Person-Pet pairs where
// each element is an anonymous type that contains a
// Pet's name and the name of the Person that owns the Pet.
var query =
people.Join(pets,
person => person,
pet => pet.Owner,
(person, pet) =>
new { OwnerName = person.Name, Pet = pet.Name }).ToList();
wohoo :)沒錯我在找什麼! – Ekaterina 2009-10-30 10:18:31
我不知道你可以通過在查詢方法周圍放置括號來獲得.ToList(),謝謝! – Haroon 2012-02-01 12:22:08
請不要調用LINQ聲明性查詢語法「LINQ語法」。它們都是「LINQ語法」。正確的命名是「查詢語法」與「方法語法」。 http://msdn.microsoft.com/en-us/library/bb397947。aspx – Pluc 2014-12-18 18:34:31