3

我需要帮助将此查询转换为 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 个新问题!:)

4

1 回答 1

3

搞定了...

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()
                        group new { rd, r, s, e, md } by new
                        {
                            rd.RouteDispatchID,
                            r.RouteNumber,
                            s.ShortDescription,
                            rd.DispatchDate,
                            rd.CreationDate,
                            e.FirstName,
                            e.LastName
                        } into g
                        select new RouteView
                        {
                            RouteDispatchID = (System.Int32)g.Key.RouteDispatchID,
                            RouteNumber = g.Key.RouteNumber,
                            Status = g.Key.ShortDescription,
                            DispatchDate = (System.DateTime)g.Key.DispatchDate,
                            CreatedDate = (System.DateTime)g.Key.CreationDate,
                            FirstName = g.Key.FirstName,
                            LastName = g.Key.LastName,
                            NumMachines = g.Count(),
                            TotalRecordCount = 0
                        };
于 2009-06-22T22:21:54.160 回答