我有3个名为Player,PlayerDetails和Team的表。我想写一个linq语句,如
from p in Player join d in PlayerDetails on p.ID equals d.PID
然后右键加入团队表,Player有一个名为TID的列,它面向Team表的ID。
我试图写一个像
这样的陈述from p in Player join d in PlayerDetails on p.ID equals d.PID into PlayerGroup
from t in team join g in PlayerGroup on t.ID equals g.p.ID
当然无法奏效。我不确定如何编写这种类型的查询语句,表左连接表2然后右连接表3。
谁可以提供帮助?
答案 0 :(得分:1)
我相信你可以这样做:
var LeftJoin =
from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
};
var RightJoin =
from t in Team
join l in LeftJoin on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};
要在一个查询中执行所有操作,我认为您可以(不测试)执行以下操作:
var RightJoin =
from t in Team
join l in
(from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
})
on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};