《.Net 系列》 - Linq & Lambda & t-Sql

select使用

sqlsql

SELECT [t0].[RealName] AS [真實名字], [t0].[Introduce] AS [自我介紹]
FROM [Blog_UserInfo] AS [t0]

Linq to sqlspa

from a in Blog_UserInfo
select new
{
    真實名字=a.RealName,
    自我介紹=a.Introduce
}

Lambdacode

Blog_UserInfo
   .Select (
      a => 
         new  
         {
            真實名字 = a.RealName, 
            自我介紹 = a.Introduce
         }
   )

orderby使用

sqlip

-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Friend%'
-- EndRegion
SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE [t0].[NickName] LIKE @p0
ORDER BY [t0].[UserId], [t0].[CreateTime] DESC

Linq to sqlstring

from a in Blog_Users
where a.NickName.Contains("Friend")
orderby a.UserId ascending,
a.CreateTime descending
select a
--或者
from a in Blog_Users
where a.NickName.Contains("Friend")
orderby a.UserId,a.CreateTime 
select a

Lambdaio

Blog_Users
   .Where (a => a.NickName.Contains ("Friend"))
   .OrderBy (a => a.UserId)
   .ThenByDescending (a => a.CreateTime)

分頁使用

sqlclass

-- Region Parameters
DECLARE @p0 Int = 2
DECLARE @p1 Int = 2
-- EndRegion
SELECT [t1].[ID], [t1].[ReceiverId], [t1].[LeaverId], [t1].[CreateTime], [t1].[Content]
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID], [t0].[ReceiverId], [t0].[LeaverId], [t0].[CreateTime], [t0].[Content]) AS [ROW_NUMBER], [t0].[ID], [t0].[ReceiverId], [t0].[LeaverId], [t0].[CreateTime], [t0].[Content]
    FROM [Blog_LeaveMsg] AS [t0]
    ) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]

Linq to sqlselect

(from a in Blog_LeaveMsgs select a).Skip(2).Take(2)

Lambda分頁

Blog_LeaveMsgs
   .Select (a => a)
   .Skip (2)
   .Take (2)

groupby(單分組)

sqlnio

-- Region Parameters
DECLARE @p0 Int = 3
-- EndRegion
SELECT [t1].[LeaverId] AS [朋友ID], [t1].[value2] AS [留言數]
FROM (
    SELECT COUNT(*) AS [value], COUNT(*) AS [value2], [t0].[LeaverId]
    FROM [Blog_LeaveMsg] AS [t0]
    GROUP BY [t0].[LeaverId]
    ) AS [t1]
WHERE [t1].[value] >= @p0

Linq to sql

from a in Blog_LeaveMsgs 
group a by a.LeaverId into b
where b.Count() >=3
select new
{
    朋友ID = b.Key,
    留言數 = b.Count()
}

Lambda

groupby(多分組)

sql

SELECT [t0].[ReceiverId] AS [接收人ID], [t0].[LeaverId] AS [留言人ID]
FROM [Blog_LeaveMsg] AS [t0]
GROUP BY [t0].[ReceiverId], [t0].[LeaverId]

Linq to sql

from a in Blog_LeaveMsgs
group a by new{a.ReceiverId,a.LeaverId} into b
select new
{
    接收人ID=b.Key.ReceiverId,
    留言人ID=b.Key.LeaverId
}

Lambda

Blog_LeaveMsgs
   .GroupBy (
      a => 
         new  
         {
            ReceiverId = a.ReceiverId, 
            LeaverId = a.LeaverId
         }
   )
   .Select (
      b => 
         new  
         {
            接收人ID = b.Key.ReceiverId, 
            留言人ID = b.Key.LeaverId
         }
   )

distinct使用

sql

SELECT DISTINCT [t0].[LeaverId]
FROM [Blog_LeaveMsg] AS [t0]

Linq to sql

(from a in Blog_LeaveMsgs
select a.LeaverId)
.Distinct()

Lambda

Blog_LeaveMsgs
   .Select (a => a.LeaverId)
   .Distinct ()

子查詢

sql

-- Region Parameters
DECLARE @p0 Int = 4
-- EndRegion
SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE EXISTS(
    SELECT NULL AS [EMPTY]
    FROM (
        SELECT COUNT(*) AS [value], [t1].[LeaverId]
        FROM [Blog_LeaveMsg] AS [t1]
        GROUP BY [t1].[LeaverId]
        ) AS [t2]
    WHERE ([t2].[LeaverId] = ([t0].[UserId])) AND ([t2].[value] >= @p0)
    )

Linq to sql

from a in Blog_Users
where
(from b in Blog_LeaveMsgs 
group b by b.LeaverId into b 
where b.Count()>=4
select b.Key).Contains(a.UserId)
select a

Lambda

Blog_Users
   .Where (
      a => 
         Blog_LeaveMsgs
            .GroupBy (b => b.LeaverId)
            .Where (b => (b.Count () >= 4))
            .Select (b => b.Key)
            .Contains ((Int32?)(a.UserId))
   )

in操做

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = 'Kimisme'
DECLARE @p1 NVarChar(1000) = 'FriendLee'
-- EndRegion
SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE [t0].[NickName] IN (@p0, @p1)

Linq to sql

from a in Blog_Users
where new string[]{"Kimisme","FriendLee"}
.Contains(a.NickName)
select a

Lambda

Blog_Users
   .Where (a => new String[] { "Kimisme", "FriendLee" } .Contains (a.NickName))

Union操做

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Lee%'
DECLARE @p1 NVarChar(1000) = '%Friend%'
-- EndRegion
SELECT [t2].[UserId], [t2].[NickName], [t2].[CreateTime]
FROM (
    SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
    FROM [Blog_User] AS [t0]
    WHERE [t0].[NickName] LIKE @p0
    UNION
    SELECT [t1].[UserId], [t1].[NickName], [t1].[CreateTime]
    FROM [Blog_User] AS [t1]
    WHERE [t1].[NickName] LIKE @p1
    ) AS [t2]

Linq to sql

(from a in Blog_Users where a.NickName.Contains("Lee") select a)
.Union
(from a in Blog_Users where a.NickName.Contains("Friend") select a)

Lambda

Blog_Users
   .Where (a => a.NickName.Contains ("Lee"))
   .Union (
      Blog_Users
         .Where (a => a.NickName.Contains ("Friend"))
   )

Concat操做

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Friend%'
DECLARE @p1 NVarChar(1000) = '%Lee%'
-- EndRegion
SELECT [t2].[UserId], [t2].[NickName], [t2].[CreateTime]
FROM (
    SELECT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
    FROM [Blog_User] AS [t0]
    WHERE [t0].[NickName] LIKE @p0
    UNION ALL
    SELECT [t1].[UserId], [t1].[NickName], [t1].[CreateTime]
    FROM [Blog_User] AS [t1]
    WHERE [t1].[NickName] LIKE @p1
    ) AS [t2]

Linq to sql

(from a in Blog_Users where a.NickName.Contains("Friend") select a)
.Concat
(from a in Blog_Users where a.NickName.Contains("Lee") select a)

Lambda

Blog_Users
   .Where (a => a.NickName.Contains ("Friend"))
   .Concat (
      Blog_Users
         .Where (a => a.NickName.Contains ("Lee"))
   )

Intersect(取交集)

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Lee%'
DECLARE @p1 NVarChar(1000) = '%Friend%'
-- EndRegion
SELECT DISTINCT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [Blog_User] AS [t1]
    WHERE ([t0].[UserId] = [t1].[UserId]) AND ([t1].[NickName] LIKE @p0)
    )) AND ([t0].[NickName] LIKE @p1)

Linq to sql

(from a in Blog_Users where a.NickName.Contains("Friend") select a)
.Intersect
(from a in Blog_Users where a.NickName.Contains("Lee") select a)

Lambda

Blog_Users
   .Where (a => a.NickName.Contains ("Friend"))
   .Intersect (
      Blog_Users
         .Where (a => a.NickName.Contains ("Lee"))
   )

Except(排除交集)

sql

-- Region Parameters
DECLARE @p0 NVarChar(1000) = '%Zhao%'
DECLARE @p1 NVarChar(1000) = '%Friend%'
-- EndRegion
SELECT DISTINCT [t0].[UserId], [t0].[NickName], [t0].[CreateTime]
FROM [Blog_User] AS [t0]
WHERE (NOT (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [Blog_User] AS [t1]
    WHERE ([t0].[UserId] = [t1].[UserId]) AND ([t1].[NickName] LIKE @p0)
    ))) AND ([t0].[NickName] LIKE @p1)

Linq to sql

(from a in Blog_Users where a.NickName.Contains("Friend") select a)
.Except
(from a in Blog_Users where a.NickName.Contains("Zhao") select a)

Lambda

Blog_Users
   .Where (a => a.NickName.Contains ("Friend"))
   .Except (
      Blog_Users
         .Where (a => a.NickName.Contains ("Zhao"))
   )

join(內鏈接)

sql

SELECT [t0].[NickName] AS [暱稱], [t1].[RealName] AS [真實名]
FROM [Blog_User] AS [t0]
INNER JOIN [Blog_UserInfo] AS [t1] ON ([t0].[UserId]) = [t1].[ID]

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
}

Lambda

Blog_Users
   .Join (
      Blog_UserInfo, 
      a => (Int32?)(a.UserId), 
      b => b.ID, 
      (a, b) => 
         new  
         {
            暱稱 = a.NickName, 
            真實名 = b.RealName
         }
   )

join(外鏈接)

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]

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
}

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
         }
   )
相關文章
相關標籤/搜索