Linq学习(四)-联合查询

一、本将主要介绍

Union、Concat、Intersect、Except的使用操作

1.Union

查询昵称中带有Friend和带有Lee的用户

Linq

(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)

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]

Lambda

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

2.Concat

查询昵称中带有Friend和昵称中带有Lee的用户,相同的用户信息不过滤

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)

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]

Lambda

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

3.Intersect(交集)

查询昵称中带有Friend的,且昵称中带有Lee的用户

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)

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)

Lambda

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

4.Except(排除交集)

查询昵称中带有Friend,并从中删除昵称中带有Zhao的用户

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)

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)

Lambda

Blog_Users
   .Where (a => a.NickName.Contains ("Friend"))
   .Except (
      Blog_Users
         .Where (a => a.NickName.Contains ("Zhao"))
   )
更多精彩内容请看:http://www.cnblogs.com/2star
原文地址:https://www.cnblogs.com/kimisme/p/5172334.html