linq中的多列连接

时间:2014-12-29 05:56:50

标签: linq

我想将此sql查询转换为linq

SELECT Students.StdId, Mark.Value
FROM Departments INNER JOIN
Mark ON Departments.DepId = Mark.DepId INNER JOIN
Students ON Departments.DepId = Students.DepId AND Mark.StdId = Students.StdId

我这样做了,但它不会工作

from s in Students
join d in Departments on s.DepId equals d.DepId
join m in Marks on new {s.StdId, d.DepId} equals new {m.StdId, m.DepId}
select new{
  SId=s.StdId,
  Value=m.Value
}

2 个答案:

答案 0 :(得分:1)

让我们试试

from s in Students
join d in Departments on s.DepId equals d.DepId
join m in Marks 
on new {StdId = s.StdId, DepId = d.DepId} equals new {StdId = m.StdId, DepId= m.DepId}
select new{
  SId=s.StdId,
  Value=m.Value
}

答案 1 :(得分:0)

怎么样

from s in Students
join m in Marks on s.StdId equals m.StdId
join d in Departments on m.DepId equals d.DepId
select new { s.StdId, m.Value };