Linq 内联左联等

我们在做SQL查询的时候经常会用到Inner Join,Left Join,笛卡尔积等等,连接方式的概念方面我想也不用给予太多解释,

我们今天的重点是让大家熟悉LINQ是如何使用Join来实现常用的表连接的。

创建测试用类:

[csharp]  view plain  copy
 
  1. class Customer  
  2. {  
  3.     public int CustomerId { get; set; }  
  4.   
  5.     public string Name { get; set; }  
  6.   
  7.     public int Age { get; set; }  
  8.   
  9. }  
  10.   
  11. class Product  
  12. {  
  13.     public int ProductId { get; set; }  
  14.   
  15.     public string Name { get; set; }  
  16.   
  17.     public string Origin { get; set; }  
  18.   
  19. }  
  20.   
  21. class Order  
  22. {  
  23.     public int OrderId { get; set; }  
  24.   
  25.     public int CustomerId { get; set; }  
  26.   
  27.     public List<Product> Products { get; set; }  
  28. }  

我们用以下例子来熟悉 Join 关键字的用法。

1.Inner Join:

[csharp]  view plain  copy
 
  1. CreateEntities();  
  2. var query = from c in customers  
  3.             join o in orders on c.CustomerId equals o.CustomerId  
  4.             where o.OrderId == 2  
  5.             select c;  
  6. foreach (var customer in query)  
  7. {  
  8.     Console.WriteLine("Id:{0}, Name:{1}", customer.CustomerId, customer.Name);  
  9. }  

 

运行结果:

  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的概念不太了解,没关系让我们通过例子来认识它:

[csharp]  view plain  copy
 
  1. CreateEntities();  
  2. var query = from c in customers  
  3.             join o in orders on c.CustomerId equals o.CustomerId into os  
  4.             select new { c, os };  
  5. foreach (var item in query)  
  6. {  
  7.     Console.WriteLine("Customer Id:{0}, Name:{1}", item.c.CustomerId, item.c.Name);  
  8.     foreach (var o in item.os)  
  9.     {  
  10.         Console.WriteLine("--Order Id:{0}", o.OrderId);  
  11.     }  
  12. }  

结果:

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里怎么实现它:

[csharp]  view plain  copy
 
  1. CreateEntities();  
  2. var query = from c in customers  
  3.             join o in orders on c.CustomerId equals o.CustomerId into os  
  4.             from o2 in os.DefaultIfEmpty(  
  5.                 new Order { OrderId = 0, CustomerId = 0, Products = new List<Product>() })  
  6.             select new { c, o2 };  
  7. foreach (var item in query)  
  8. {  
  9.     Console.WriteLine("Customer Id:{0}, Name:{1}--Order Id:{0}",  
  10.         item.c.CustomerId, item.o2.OrderId);  
  11. }  

结果:

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查询表达式中用的不是很多。

转载于:https://www.cnblogs.com/libaoli/p/5261141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值