LINQ 查询表达式join 关键字用法:
我在这里先创建的实验用例:
03 |
public int CustomerId { get ; set ; } |
04 |
public string Name { get ; set ; } |
05 |
public int Age { get ; set ; } |
10 |
public int ProductId { get ; set ; } |
11 |
public string Name { get ; set ; } |
12 |
public string Origin { get ; set ; } |
17 |
public int OrderId { get ; set ; } |
18 |
public int CustomerId { get ; set ; } |
19 |
public List<Product> Products { get ; set ; } |
21 |
static List<Customer> customers; |
22 |
static List<Product> products; |
23 |
static List<Order> orders; |
24 |
public static void CreateEntities() |
26 |
customers = new List<Customer>{ |
27 |
new Customer(){ CustomerId = 1, Name = "CA" , Age=13}, |
28 |
new Customer(){ CustomerId = 2, Name = "CB" , Age=13}, |
29 |
new Customer(){ CustomerId = 3, Name = "CC" , Age=13}, |
30 |
new Customer(){ CustomerId = 4, Name = "CD" , Age=13} |
33 |
products = new List<Product>{ |
34 |
new Product(){ ProductId = 1, Name = "PA" , Origin= "P1" }, |
35 |
new Product(){ ProductId = 2, Name = "PB" , Origin= "P2" }, |
36 |
new Product(){ ProductId = 3, Name = "PC" , Origin= "P1" }, |
37 |
new Product(){ ProductId = 4, Name = "PD" , Origin= "P3" } |
40 |
orders = new List<Order>{ |
41 |
new Order(){ OrderId = 1 , CustomerId =1, |
42 |
Products = new List<Product>{ |
43 |
new Product(){ ProductId = 2, Name = "PB" , Origin= "P2" }, |
44 |
new Product(){ ProductId = 3, Name = "PC" , Origin= "P1" } |
46 |
new Order(){ OrderId = 2 , CustomerId =1, |
47 |
Products = new List<Product>{ |
48 |
new Product(){ ProductId = 3, Name = "PC" , Origin= "P1" }, |
49 |
new Product(){ ProductId = 4, Name = "PD" , Origin= "P3" } |
51 |
new Order(){ OrderId = 3 , CustomerId =3, |
52 |
Products = new List<Product>{ |
53 |
new Product(){ ProductId = 4, Name = "PD" , Origin= "P3" } |
55 |
new Order(){ OrderId = 4 , CustomerId =2, |
56 |
Products = new List<Product>{ |
57 |
new Product(){ ProductId = 1, Name = "PA" , Origin= "P1" }, |
58 |
new Product(){ ProductId = 4, Name = "PD" , Origin= "P3" } |
我们在做SQL查询的时候经常会用到Inner Join,Left Join,笛卡尔积等等,连接方式的概念方面我想也不用给予太多解释,
我们今天的重点是让大家熟悉LINQ是如何使用Join来实现常用的表连接的。
我们用以下例子来熟悉 Join 关键字的用法。
1.Inner Join:
2 |
var query = from c in customers |
3 |
join o in orders on c.CustomerId equals o.CustomerId |
6 |
foreach (var customer in query) |
8 |
Console.WriteLine( "Id:{0}, Name:{1}" , customer.CustomerId, customer.Name); |
运行结果:
Id:1, Name:CA
上面这个是常见的内连接的例子,和SQL语法也很相似,但有以下几点要注意:
(1).连接条件: c.CustomerId equals o.CustomerId 只能使用 equals 不能用 =,==,等于 等表示。
以为LINQ的设计者认为 几乎所有的连接条件都是 = 条件不会出现 >,<,!= 等情况因此使用了个关键字来描述表连接条件。
(2).条件顺序:c.CustomerId equals o.CustomerId ,range variable: c 和b之前的顺序不能颠倒。
2.Group Join:
也许大家对Group Join的概念不太了解,没关系让我们通过例子来认识它:
02 |
var query = from c in customers |
03 |
join o in orders on c.CustomerId equals o.CustomerId into os |
05 |
foreach (var item in query) |
07 |
Console.WriteLine( "Customer Id:{0}, Name:{1}" , item.c.CustomerId, item.c.Name); |
08 |
foreach (var o in item.os) |
10 |
Console.WriteLine( "--Order Id:{0}" , o.OrderId); |
结果:
Customer Id:1, Name:CA
--Order Id:1
--Order Id:2
Customer Id:2, Name:CB
--Order Id:4
Customer Id:3, Name:CC
--Order Id:3
Customer Id:4, Name:CD
Press any key to continue . . .
以上查询返回的结果:和Group By 返回的结果非常的相似:一个KEY对象对应一个集合。
要实现Group Join我们要引入一个关键字:into
但使用时要注意一下几点:
(1).使用into 关键字后 join 后面的 range variable:o 在后面的表达式块中就失去了作用域。
(2).range variable:os 通常情况下都是IEnumerable<T>类型的。
3.Left Join:
Left Join 我们在SQL里经常用到,让我们来看看LINQ里怎么实现它:
02 |
var query = from c in customers |
03 |
join o in orders on c.CustomerId equals o.CustomerId into os |
04 |
from o2 in os.DefaultIfEmpty( |
05 |
new Order { OrderId = 0, CustomerId = 0, Products = new List<Product>() }) |
07 |
foreach (var item in query) |
09 |
Console.WriteLine( "Customer Id:{0}, Name:{1}--Order Id:{0}" , |
10 |
item.c.CustomerId, item.o2.OrderId); |
结果:
Customer Id:1, Name:1--Order Id:1
Customer Id:1, Name:2--Order Id:1
Customer Id:2, Name:4--Order Id:2
Customer Id:3, Name:3--Order Id:3
Customer Id:4, Name:0--Order Id:4
Press any key to continue . . .
我们可以看到Left Outer Join 的语法进一步的复杂化了,结果也有细微的不同。
(1).从语法上:
from o2 in os.DefaultIfEmpty(
new Order { OrderId = 0, CustomerId = 0, Products = new List<Product>() })
主要区别在于以上者1句语句。查询方法DefaultIfEmpty 用于定义当查询记录为空时,预定义默认值。再从其集合中取出子元素。
(2).从结果上:
我们在遍历查询结果时可以发现Left Join相似于Inner Join结果都是“平面”的,然而Group Join返回的结果具有层次性。
题外:
由于C#是面向对象的,往往会通过对象与对象间的外系来实现数据间关系。有时表达2个之间的关系也可以不使用Join关键字,
因此Join关键字其实在实际LINQ查询表达式中用的不是很多。
Lambda表达式如下:
- TUsers.GroupJoin(
- TUserInfo,
- u => u.UserID,
- ui => ui.UserID,
- (u, ui) => new { u, ui }).Select(o => o)