LINQ学习笔记:Join和Group Join


连接Join

主要方法:

Join:  应用一个搜寻策略去匹配两个集合中的元素, 并返回一个扁平的结果集, SQL对应语法为INNER JOIN

Group Join: 同上, 但返回的是一个层级的结果集, SQL对应语法为INNER JOIN, LEFT OUTER JOIN

概要

Join和GroupJoin将两个输入序列编织成一个单一的输出序列, Join返回一个扁平的输出结果集, GroupJoin则返回一个层级结果集.

Join和GroupJoin提供了Select与SelectMany之外的另一个选择.  Join和GroupJoin的优势在于针对本地查询更加高效,因为它首先将内部序列加载到一个lookup目录当中,避免重复枚举每一个内部元素. 它的劣势在于只能对应于INNER JOIN和LEFT OUTER JOIN, 交叉连接和非等连接依然还是需要Select和SelectMany. 而对于LINQ to SQL, Join和GroupJoin对比Select和SelectMany并没有提供任何额外的好处.

Join

Join操作符执行一个内连接(inner join), 输出一个扁平序列

最简单的演示Join用处的做法是使用LINQ to SQL, 以下的查询列出所有的客户以及他们的订单信息而没有使用关联属性

   1: IQueryable<string> query =
   2:  
   3:   from c in dataContext.Customers
   4:  
   5:   join p in dataContext.Purchases
   6:  
   7:     on c.ID equals p.CustomerID
   8:  
   9:   select c.Name + ” bought a “ + p.Description;

 

结果与我们使用SelectMany查询得到的结果一致

要了解Join相对于SelectMany额外的好处, 我们必须将它转换为本地查询, 以下的例子先将所有的客户和采购订单转换为数组, 然后再做进一步的查询:

   1: Customer[] customers = dataContext.Customers.ToArray( );
   2:  
   3: Purchase[] purchases = dataContext.Purchases.ToArray( );
   4:  
   5: var slowQuery =
   6:  
   7:   from c in customers
   8:  
   9:   from p in purchases where c.ID == p.CustomerID
  10:  
  11:   select c.Name + ” bought a “ + p.Description;
  12:  
  13: var fastQuery =
  14:  
  15:   from c in customers
  16:  
  17:   join p in purchases on c.ID equals p.CustomerID
  18:  
  19:   select c.Name + ” bought a “ + p.Description;

 

虽然两种方式返回的结果集是一样的, 但是Join查询执行得更快一些, 因为它在Enumerable当中的实现预加载了内联集合(purchases)到一个有键的字典中

Join执行一个内连接操作, 这意味着那些没有采购订单的客户将被排除在输出结果之外. 使用inner join, 你可以将inner和outer序列互换, 并且仍然可以得到同样的结果:

   1: from p in purchases
   2:  
   3:    join c in customers on p.CustomerID equals c.ID

 

我们可以增加更多的join语句到相同的查询中, 例如, 假设每个采购订单包含一或多个的采购明细, 我们可以像下面这样将他们连接在一起:

   1: from c in customers
   2:  
   3: join p in purchases on c.ID equals p.CustomerID
   4:  
   5: join pi in purchaseItems on p.ID equals pi.PurchaseID

 

Purchases在第一个连接中扮演了inner序列, 而在第二个连接中则扮演了outer序列的角色, 我们可以使用嵌套foreach得到相同的结果, 但是效率不高:

   1: foreach (Customer c in customers)
   2:  
   3:   foreach (Purchase p in purchases)
   4:  
   5:     if (c.ID == p.CustomerID)
   6:  
   7:       foreach (PurchaseItem pi in purchaseItems)
   8:  
   9:         if (p.ID == pi.PurchaseID)
  10:  
  11:           Console.WriteLine (c.Name + “,” + p.Price +
  12:  
  13:                                       “,” + pi.Detail);

 

多主键连接

我们可以使用匿名类型来进行多主键链接操作:

   1: from x in seqX
   2:  
   3: join y in seqY on new { K1 = x.Prop1, K2 = x.Prop2 }
   4:  
   5:            equals new { K1 = y.Prop3, K2 = y.Prop4 }

 

为了能够运行这个查询, 两个匿名类型的结构必须是相同的. 编译器会将它们实现为相同的内部类型, 因此多主键链接能够运行.

Lambda方式的连接

以下的示例使用了复合查询语法:

   1: from c in customers
   2:  
   3: join p in purchases on c.ID equals p.CustomerID
   4:  
   5: select new { c.Name, p.Description, p.Price };

 

使用Lambda表达式的话则可以改成这样:

   1: customers.Join (                // outer collection
   2:  
   3:              purchases,                // inner collection
   4:  
   5:              c => c.ID,                // outer key selector
   6:  
   7:              p => p.CustomerID,        // inner key selector
   8:  
   9:              (c, p) => new             // result selector
  10:  
  11:                 { c.Name, p.Description, p.Price }
  12:  
  13:        );

 

最后的结果选择器表达式创建了输出序列中的每一个元素, 如果你还有额外的查询语句需要去执行, 例如orderby:

   1: from c in customers
   2:  
   3: join p in purchases on c.ID equals p.CustomerID
   4:  
   5: orderby p.Price
   6:  
   7: select c.Name + ” bought a “ + p.Description;

 

在Lambda方式中, 我们就必须在结果选择器表达式中去生成一个临时的匿名类型. 这样可以保持c和p在同一个join作用范围内:

   1: customers.Join (               // outer collection
   2:  
   3:       purchases,               // inner collection
   4:  
   5:       c => c.ID,               // outer key selector
   6:  
   7:       p => p.CustomerID,       // inner key selector
   8:  
   9:       (c, p) => new { c, p } ) // result selector
  10:  
  11:  .OrderBy (x => x.p.Price)
  12:  
  13:  .Select (x => x.c.Name + ” bought a “
  14:  
  15:                         + x.p.Description);

 

可以看得出来复合查询语法更加的直观一点, 这也是在使用joining操作时推荐的做法.


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3