LINQ内加入VS左加入 [英] LINQ Inner-Join vs Left-Join
问题描述
使用扩展语法我试图创建一个使用LINQ在两个列表中,我有一个左联接。以下是从微软的帮助,但我已经修改了它表明,宠物列表中没有的元素。什么我结束了同是0元素的列表。我认为,这是因为内部联接正在发生。我想结束了是3元(3 Person对象)与填充了缺少的元素空数据的列表。即左加入。这可能吗?
人马格努斯=新的Person {名称=Hedlund的,马格努斯};
人特里=新的Person {名称=亚当斯,特里};
夏洛特人=新的Person {名称=韦斯,夏洛特};
//宠物大麦=新宠物{名称=大麦,业主=特里};
//宠物靴=新宠物{名称=靴子,业主=特里};
//宠物晶须=新宠物{名称=威威,业主=夏洛特};
//宠物菊花=新宠物{名称=雏菊,业主=马格努斯};
名单<人>人=新的名单,其中,人物> {马格努斯,特里,夏洛特};
//名单,其中,宠物>宠物=新的名单,其中,宠物> {大麦,靴子,胡须,菊花};
名单<宠物>宠物=新的名单,其中,宠物>();
//创建人物,宠物对,其中一个列表
//每一个元素是一个匿名类型包含
//宠物的名字,并且拥有宠物的人的名字。
VAR的查询=
people.Join(宠物,
人=>人,
宠物=> pet.Owner,
(人,宠物)=>
新{OWNERNAME = person.Name,宠物= pet.Name})了ToList()。
我想,如果你想使用扩展方法,你需要使用的群组加入
VAR的查询=
people.GroupJoin(宠物,
人=>人,
宠物=> pet.Owner,
(人,petCollection)=>
新{OWNERNAME = person.Name,
宠物= PetCollection.Select(P => p.Name)
.DefaultIfEmpty()}
).ToList();
您可能不得不玩的选择EX pression。我不知道它会给你想你想的情况下,你有一个1对多的关系。
我认为这是一个更容易一些与LINQ查询语法
VAR的查询=(从人context.People
宠物一起在context.Pets的人等于pet.Owner
进入tempPets
从tempPets.DefaultIfEmpty宠物()
选择新{OWNERNAME = person.Name,宠物= pets.Name})
.ToList();
Using extension syntax I'm trying to create a left-join using LINQ on two lists that I have. The following is from the Microsoft help but I've modified it to show that the pets list has no elements. What I'm ending up with is a list of 0 elements. I assume that this is because an inner-join is taking place. What I want to end up with is a list of 3 elements (the 3 Person objects) with null data filled in for the missing elements. i.e. a Left-Join. Is this possible?
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();
I think if you want to use extension methods you need to use the GroupJoin
var query =
people.GroupJoin(pets,
person => person,
pet => pet.Owner,
(person, petCollection) =>
new { OwnerName = person.Name,
Pet = PetCollection.Select( p => p.Name )
.DefaultIfEmpty() }
).ToList();
You may have to play around with the selection expression. I'm not sure it would give you want you want in the case where you have a 1-to-many relationship.
I think it's a little easier with the LINQ Query syntax
var query = (from person in context.People
join pet in context.Pets on person equals pet.Owner
into tempPets
from pets in tempPets.DefaultIfEmpty()
select new { OwnerName = person.Name, Pet = pets.Name })
.ToList();
这篇关于LINQ内加入VS左加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!