Linq Coding -- Part Six (Join之分组联接)
Join 子句有三种最常见的联接类型:内部联接,分组联接,左外部联接
本次介绍:分组联接
分组联接:见名知意,它本质上是一个对象数组序列。说白了就是结果序列会组织为多个组形式数据进行返回。
即可术语描述的话,就是会产生一个分层的结果序列,此序列拿第一个集合中的每个元素与第二个集合中的一组相关元素进行配对,如果找不到就返回空数组。那基本上分组联接和内部联接基本差不多。 另外就是在分组联接中没有等效的关系。
* 分组联接非常适合于使用 LINQ to XML 来创建 XML。
public class JoinUsingLinqPartSecInto : Interface
{
Interface Members
}
{
Interface Members
}
*以下这三种查询基本上都是等效查询(这也是使用分组联接实现内部联接的一例)
querythree使用不带 into 子句的 join 子句执行内部联接所获得的结果集。
1 var queryone = from person in people
2 join pet in pets
3 on person
4 equals pet.Owner
5 into nonameList
6 select new { OwnerName = person.FirstName, Pets = nonameList };
7
8 var querytwo = from person in people
9 join pet in pets
10 on person
11 equals pet.Owner
12 into nonnamelist
13 from subpet in nonnamelist
14 select new { OwnerName = person.FirstName, PetName = subpet.Name };
15
16 var querythree = from person in people
17 join pet in pets on person equals pet.Owner
18 select new { OwnerName = person.FirstName, PetName = pet.Name };
2 join pet in pets
3 on person
4 equals pet.Owner
5 into nonameList
6 select new { OwnerName = person.FirstName, Pets = nonameList };
7
8 var querytwo = from person in people
9 join pet in pets
10 on person
11 equals pet.Owner
12 into nonnamelist
13 from subpet in nonnamelist
14 select new { OwnerName = person.FirstName, PetName = subpet.Name };
15
16 var querythree = from person in people
17 join pet in pets on person equals pet.Owner
18 select new { OwnerName = person.FirstName, PetName = pet.Name };
LINQ Coding 目录
- Linq Coding -- Part One
- Linq Coding -- Part Two[标准查询运算符]
- Linq Coding -- Part Three [Let子句]
- Linq Coding -- Part Four[Concat应用]
- Linq Coding -- Part Five (Join之内部联接查询)
- Linq Coding -- Part Six (Join之分组联接)
- Linq Coding -- Part Seven (Join之左外部联接、DefaultIfEmpty、GroupJoin)
- Linq Coding -- Part Eight (Equals Topic)