我需要帮助将这个查询转换为linq to sql:
select
rd.RouteDispatchID,
r.RouteNumber,
s.ShortDescription Status,
rd.DispatchDate,
rd.CreationDate CreatedDate,
e.FirstName,
e.LastName,
count(md.MachineDispatchID) NumMachines
from
dbo.RouteDispatch rd
inner join dbo.Route r on rd.RouteID = r.RouteID
inner join dbo.Reference s on rd.StatusCodeReferenceID = s.ReferenceID
inner join dbo.Employee e on rd.CreatedByEmployeeID = e.EmployeeID
left join dbo.MachineDispatch md on rd.RouteDispatchID = md.RouteDispatchID and md.IsSelected = 1
以下是我目前完成的内容,但我无法弄清如何使分组计数(group by/count)工作,我的Linqer工具也无法解决这个问题...
var query = from rd in db.RouteDispatches
join r in db.Routes on rd.RouteID equals r.RouteID
join s in db.References on new { StatusCodeReferenceID = rd.StatusCodeReferenceID } equals new { StatusCodeReferenceID = s.ReferenceID }
join e in db.Employees on new { CreatedByEmployeeID = rd.CreatedByEmployeeID } equals new { CreatedByEmployeeID = e.EmployeeID }
join md in db.MachineDispatches
on new { RouteDispatchID = rd.RouteDispatchID, IsSelected = true }
equals new { RouteDispatchID = md.RouteDispatchID, IsSelected = md.IsSelected.Value } into md_join
from md in md_join.DefaultIfEmpty()
select new RouteView {
RouteDispatchID = rd.RouteDispatchID,
RouteNumber = r.RouteNumber,
Status = s.ShortDescription,
DispatchDate = rd.DispatchDate.Value,
CreatedDate = rd.CreationDate.Value,
FirstName = e.FirstName,
LastName= e.LastName,
NumMachines = 0//.Count()
};
感谢能够解决这个问题的人,我还是linq to sql的新手,每天都会有2-3个新问题!!:)