Linq学习(五)-多表连接

2018-06-18 02:14:17来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

本将主要介绍 内连接与 外连接

1.join

Linq to sql

from a in Blog_Users
join b in Blog_UserInfo
on  a.UserId equals b.ID
select new
{
    昵称=a.NickName,
    真实名=b.RealName
}

sql

SELECT [t0].[NickName] AS [昵称], [t1].[RealName] AS [真实名]
FROM [Blog_User] AS [t0]
INNER JOIN [Blog_UserInfo] AS [t1] ON ([t0].[UserId]) = [t1].[ID]

Lambda

Blog_Users
   .Join (
      Blog_UserInfo, 
      a => (Int32?)(a.UserId), 
      b => b.ID, 
      (a, b) => 
         new  
         {
            昵称 = a.NickName, 
            真实名 = b.RealName
         }
   )

2.外连接

Linq to sql

from a in Blog_Users
join b in Blog_UserInfo
on  a.UserId equals b.ID
into ab
from c in ab.DefaultIfEmpty()
select new
{
    昵称=a.NickName,
    真实名=c.RealName
}

sql

SELECT [t0].[NickName] AS [昵称], [t1].[RealName] AS [真实名]
FROM [Blog_User] AS [t0]
LEFT OUTER JOIN [Blog_UserInfo] AS [t1] ON ([t0].[UserId]) = [t1].[ID]

Lambda

Blog_Users
   .GroupJoin (
      Blog_UserInfo, 
      a => (Int32?)(a.UserId), 
      b => b.ID, 
      (a, ab) => 
         new  
         {
            a = a, 
            ab = ab
         }
   )
   .SelectMany (
      temp0 => temp0.ab.DefaultIfEmpty (), 
      (temp0, c) => 
         new  
         {
            昵称 = temp0.a.NickName, 
            真实名 = c.RealName
         }
   )

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:EasyUI系列学习(三)-Draggable(拖动)

下一篇:WinForm数据源分页技术