LINQ内加入VS左加入

编程入门 行业动态 更新时间:2024-10-28 06:23:05
本文介绍了LINQ内加入VS左加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用扩展语法我试图创建一个使用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左加入

本文发布于:2023-06-03 10:01:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!