在進行資料庫操作時,經常會涉及到多表聯合查詢,這時就需要用到join。而連線也分為以下兩種:
idgroupname1a
2b3c
idusername
groupid1王1
11王22
1王34
對應sql語句為from g in groups
join u in users
on g.id equals u.groupid
select new
結果如下:select [t0].[groupname], [t1].[username]
from [group] as [t0]
inner join [user] as [t1] on ([t0].[id]) = [t1].[groupid]
groupname
usernamea張1
b張2
對應sql為from g in groups
join u in users
on g.id equals u.groupid
into grp
from grp in grp.defaultifempty()
select new
結果為: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]
groupname
usernamea王1
b王2c
對應sqlfrom u in users
join g in groups
on u.groupid equals g.id
into grp
from grp in grp.defaultifempty()
select new
結果為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])
groupname
usernamea王1
b王2王3
對應sqlvar a=from g in groups
join u in users
on g.id equals u.groupid
into grp
from grp in grp.defaultifempty()
select new ;
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 ;
var c=a.concat(b).distinct();
c.dump();
結果為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]
groupname
username王3a
王1b王2c
Linq中的連線 join
linq中連線主要有組連線 內連線 左外連線 交叉連線四種。各個用法如下。1 組連線 組連線是與分組查詢是一樣的。即根據分組得到結果。如下例,根據publisther分組得到結果。使用組連線的查詢語句如下 使用組連線 var groupquery from publisher in sampleda...
Linq中的連線 join
linq中連線主要有組連線 內連線 左外連線 交叉連線四種。各個用法如下。1 組連線 組連線是與分組查詢是一樣的。即根據分組得到結果。如下例,根據publisther分組得到結果。使用組連線的查詢語句如下 使用組連線 var groupquery from publisher in sampleda...
linq例項 Join的使用(四)
測試的class。public class user private set public string lastname private set public int id private set public user string firstname,string lastname,int i...