LINQ 内连接与左连接
使用扩展语法,我尝试使用 LINQ 在我拥有的两个列表上创建左连接。 以下内容来自 Microsoft 帮助,但我对其进行了修改以显示宠物列表没有元素。 我最终得到的是 0 个元素的列表。 我认为这是因为正在发生内部联接。 我想要最终得到的是 3 个元素(3 个 Person 对象)的列表,其中填充了缺失元素的空数据。 即左连接。 这可能吗?
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为如果您想使用扩展方法,您需要使用 GroupJoin
您可能需要尝试一下选择表达式。 我不确定在你有一对多关系的情况下它会给你想要的。
我认为使用 LINQ 查询语法会更容易一些
I think if you want to use extension methods you need to use the GroupJoin
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
您需要将连接的对象放入一个集合中,然后应用 DefaultIfEmpty,如 JPunyon 所说:
输出:
You need to get the joined objects into a set and then apply DefaultIfEmpty as JPunyon said:
Outputs:
当遇到同样的问题时,我出现以下错误消息:
连接子句中的表达式之一的类型不正确。 对“GroupJoin”的调用中类型推断失败。
当我使用相同的属性名称时解决了,它起作用了。
(...)
(...)
I the following error message when faced this same problem:
The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.
Solved when I used the same property name, it worked.
(...)
(...)
这是 Fabrice(《LINQ in Action》的作者)刚刚发布的一篇很好的博客文章,其中涵盖了我提出的问题中的材料。 我将其放在这里供参考,因为问题的读者会发现这很有用。
将 LINQ 查询从查询语法转换为方法/运算符语法
Here's a good blog post that's just been posted by Fabrice (author of LINQ in Action) which covers the material in the question that I asked. I'm putting it here for reference as readers of the question will find this useful.
Converting LINQ queries from query syntax to method/operator syntax
LINQ 中的左连接可以使用 DefaultIfEmpty() 方法实现。 不过,我没有适合您情况的确切语法...
实际上,我认为如果您只是在查询中将 pets 更改为 pets.DefaultIfEmpty() ,它可能会起作用...
编辑:我真的不应该在迟到时回答问题...
Left joins in LINQ are possible with the DefaultIfEmpty() method. I don't have the exact syntax for your case though...
Actually I think if you just change pets to pets.DefaultIfEmpty() in the query it might work...
EDIT: I really shouldn't answer things when its late...
如果您确实有数据库,这是最简单的方法:
If you actually have a database, this is the most-simple way: