Linq表連接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)
阿新 • • 發佈:2017-06-07
iar value fsm wdcp esc max als tla plc
轉載http://www.cnblogs.com/shenqiboy/p/3260105.html
我們知道在SQL中一共有五種JOIN操作:INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN
內連接、
Sql:
SELECT [t0].[GroupName], [t1].[UserName]
FROM [Group] AS [t0]
INNER JOIN [User] AS [t1] ON ([t0].[Id]) = [t1].[GroupId]
Linq to Sql:
from g in Groups
join u in Users
on g.Id equals u.GroupId
select new { GroupName=g.GroupName, UserName=u.UserName}
Lambda:
Groups.Join
(
Users,
g => (Int32?)(g.Id),
u => u.GroupId,
(g, u) =>
new
{
GroupName = g.GroupName,
UserName = u.UserName
}
)
左連接、
Sql:
-- Region Parameters
DECLARE @p0 NVarChar(1000) = ‘‘
-- EndRegion
SELECT [t0].[GroupName],
(CASE
WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(50),@p0)
ELSE [t2].[UserName]
END) AS [UserName]
FROM [Group] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[UserName], [t1].[GroupId]
FROM [User] AS [t1]
) AS [t2] ON ([t0].[Id]) = [t2].[GroupId]
Linq to Sql:
from g in Groups
join u in Users
on g.Id equals u.GroupId
into Grp
from grp in Grp.DefaultIfEmpty()
select new { GroupName=g.GroupName, UserName=(grp==null)?"":grp.UserName}
Lambda:
Groups.GroupJoin (
Users,
g => (Int32?)(g.Id),
u => u.GroupId,
(g, Grp) =>
new
{
g = g,
Grp = Grp
}
) .SelectMany (
temp0 => temp0.Grp.DefaultIfEmpty (),
(temp0, grp) =>
new
{
GroupName = temp0.g.GroupName,
UserName = (grp == null) ? "" : grp.UserName
}
)
右連接、
Sql:
-- Region Parameters
DECLARE @p0 NVarChar(1000) = ‘‘
-- EndRegion
SELECT
(CASE
WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(50),@p0)
ELSE [t2].[GroupName]
END) AS [GroupName], [t0].[UserName]
FROM [User] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[Id], [t1].[GroupName]
FROM [Group] AS [t1]
) AS [t2] ON [t0].[GroupId] = ([t2].[Id])
Linq to Sql:
from u in Users
join g in Groups
on u.GroupId equals g.Id
into Grp
from grp in Grp.DefaultIfEmpty()
select new { GroupName=(grp==null)?"":grp.GroupName, UserName=u.UserName}
Lambda:
Users.GroupJoin (
Groups,
u => u.GroupId,
g => (Int32?)(g.Id),
(u, Grp) =>
new
{
u = u,
Grp = Grp
}
).SelectMany (
temp0 => temp0.Grp.DefaultIfEmpty (),
(temp0, grp) =>
new
{
GroupName = (grp == null) ? "" : grp.GroupName,
UserName = temp0.u.UserName
}
)
全連接、
Sql:
-- Region Parameters
DECLARE @p0 NVarChar(1000) = ‘‘
DECLARE @p1 NVarChar(1000) = ‘‘
-- EndRegion
SELECT DISTINCT [t7].[GroupName], [t7].[value] AS [UserName]
FROM (
SELECT [t6].[GroupName], [t6].[value]
FROM (
SELECT [t0].[GroupName],
(CASE
WHEN [t2].[test] IS NULL THEN CONVERT(NVarChar(50),@p0)
ELSE [t2].[UserName]
END) AS [value]
FROM [Group] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[UserName], [t1].[GroupId]
FROM [User] AS [t1]
) AS [t2] ON ([t0].[Id]) = [t2].[GroupId]
UNION ALL
SELECT
(CASE
WHEN [t5].[test] IS NULL THEN CONVERT(NVarChar(50),@p1)
ELSE [t5].[GroupName]
END) AS [value], [t3].[UserName]
FROM [User] AS [t3]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t4].[Id], [t4].[GroupName]
FROM [Group] AS [t4]
) AS [t5] ON [t3].[GroupId] = ([t5].[Id])
) AS [t6]
) AS [t7]
Linq to Sql:
var a=from g in Groups
join u in Users
on g.Id equals u.GroupId
into Grp
from grp in Grp.DefaultIfEmpty()
select new { GroupName=g.GroupName, UserName=(grp==null)?"":grp.UserName};
var b=from u in Users
join g in Groups
on u.GroupId equals g.Id
into Grp
from grp in Grp.DefaultIfEmpty()
select new { GroupName=(grp==null)?"":grp.GroupName, UserName=u.UserName};
var c=a.Concat(b).Distinct();
c.Dump();
Lambda:
Groups
.GroupJoin (
Users,
g => (Int32?)(g.Id),
u => u.GroupId,
(g, Grp) =>
new
{
g = g,
Grp = Grp
}
)
.SelectMany (
temp0 => temp0.Grp.DefaultIfEmpty (),
(temp0, grp) =>
new
{
GroupName = temp0.g.GroupName,
UserName = (grp == null) ? "" : grp.UserName
}
)
.Concat (
Users
.GroupJoin (
Groups,
u => u.GroupId,
g => (Int32?)(g.Id),
(u, Grp) =>
new
{
u = u,
Grp = Grp
}
)
.SelectMany (
temp2 => temp2.Grp.DefaultIfEmpty (),
(temp2, grp) =>
new
{
GroupName = (grp == null) ? "" : grp.GroupName,
UserName = temp2.u.UserName
}
)
)
.Distinct ()
笛卡爾積、
Sql:
SELECT [t0].[GroupName], [t1].[UserName]
FROM [Group] AS [t0], [User] AS [t1]
Linq to Sql:
from g in Groups
from u in Users
select new { GroupName=g.GroupName, UserName=u.UserName}
Lambda:
Groups.SelectMany
(
g => Users,
(g, u) =>
new
{
GroupName = g.GroupName,
UserName = u.UserName
}
)
Linq表連接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)