C#中的LINQ内加入 [英] C# Linq Inner Join

查看:145
本文介绍了C#中的LINQ内加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想选择谁是有宠物的人。



当我执行查询

  VAR的查询=从对人们
加入宠物

点页上等于pts.Owner到GRP
选择新{GRP = GRP ,PersonName的= p.FirstName};



宠物也都被选择的人都没有。



我的列表是

 人[] = PRN新的人[3]; 
PRN [0] =新的Person();
PRN [0] = .FirstName乔恩;
PRN [0] .LastName =飞碟;

PRN [1] =新的Person();
PRN [1] .FirstName =马克;
PRN [1] .LastName =Gravell;

PRN [2] =新的Person();
PRN [2] .FirstName =亚历克斯;
PRN [2] .LastName =格罗弗;

名单,LT;人>人=新的List<&人GT;();

的foreach(在PRN者P)
{
people.Add(对);
}

宠物[] PT =新宠物[​​3];

PT [0] =新宠物();
PT [0] .Name点=Zonny;
PT [0] = .Owner人[0];

PT [1] =新宠物();
PT [1] .Name点=Duggie;
PT [1] .Owner =人[0];

PT [2] =新宠物();
PT [2] .Name点=Zoggie;
角[2] .Owner =人[1];

名单,LT;宠物及GT;宠物=新的List<宠物及GT;();
的foreach(宠物p在PT)
{
pets.Add(P);
}


解决方案

这是因为您在使用加入到... 这确实一批加入。你只是想要一个正常的加入:

  VAR的查询=(上接第人们
加入点在宠物页上等于pts.Owner
选择p).Distinct();



另外,如果你想与宠物的人,他们的主人,你可以这样做:

  VAR的查询= pets.GroupBy(PET => pet.Owner)
。选择(X =>新建{业主= x.Key,宠物= x.ToList()});

这会给出一个结果,你可以得到每一位业主和他们的宠物,但只有人谁有无的宠物。



如果你想要别的东西,让我们知道...



顺便说一句,现在是学习对象和集合初始化的好时机。这里有一个简单的初始化列表方式,例如:

 清单<&人GT;人=新的List<&人GT; 
{
新的Person {名字=乔恩,姓氏=斯基特},
新的Person {名字=马克,姓氏=Gravell},
新人{名字=亚历克斯,姓氏=格罗弗},
};



紧凑得多:)



编辑:交叉连接很简单:

  VAR的查询=从一个人从宠物的人
在宠物
选择新的{人,宠物};

左联接使用组加入有效地效仿。因为它听起来像是你有C#的深入,我建议你详细阅读第11章)


I want to select the persons only who are having pets.

when I execute the query

var query = from p in people
                        join
                        pts in pets
                        on p equals pts.Owner into grp
                        select new {grp=grp,PersonName=p.FirstName};

Person does not have pet also get selected.

My Lists are

Person[] prn = new Person[3];
prn[0] = new Person();
prn[0].FirstName = "Jon";
prn[0].LastName = "Skeet";

prn[1] = new Person();
prn[1].FirstName = "Marc";
prn[1].LastName = "Gravell";

prn[2] = new Person();
prn[2].FirstName = "Alex";
prn[2].LastName = "Grover";

List<Person> people = new List<Person>();

 foreach (Person p in prn)
 {
     people.Add(p);
 }

 Pet[] pt = new Pet[3];

 pt[0] = new Pet();
 pt[0].Name = "Zonny";
 pt[0].Owner = people[0];

pt[1] = new Pet();
pt[1].Name = "Duggie";
pt[1].Owner = people[0];

pt[2] = new Pet();
pt[2].Name = "Zoggie";
pt[2].Owner = people[1];

List<Pet> pets=new List<Pet>();
 foreach(Pet p in pt)
 {
    pets.Add(p);
 }

解决方案

That's because you're using join ... into which does a group join. You just want a normal join:

var query = (from p in people
             join pts in pets on p equals pts.Owner
             select p).Distinct();

Alternatively, if you want the people with pets, and their owners, you could do something like:

var query = pets.GroupBy(pet => pet.Owner)
                .Select(x => new { Owner = x.Key, Pets = x.ToList() });

That will give a result where you can get each owner and their pets, but only for people who have pets.

If you want something else, let us know...

By the way, now would be a good time to learn about object and collection initializers. Here's a simpler way to initialize your people list, for example:

List<Person> people = new List<Person>
{
    new Person { FirstName = "Jon", LastName = "Skeet" },
    new Person { FirstName = "Marc", LastName = "Gravell" },
    new Person { FirstName = "Alex", LastName = "Grover" },
};

Much more compact :)

EDIT: A cross join is easy:

var query = from person in people
            from pet in pets
            select new { person, pet };

Left joins are effectively emulated using group joins. As it sounds like you've got C# in Depth, I suggest you read chapter 11 thoroughly :)

这篇关于C#中的LINQ内加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆