使用Where和GroupBY将sql转换为linq

时间:2016-01-28 15:33:47

标签: c# sql linq

SQL:

SELECT  
p.PipelineID
, MAX(PipelineJobStatus.CreatedTimeStamp)
, Client.ClientName
, FCO.NameFCO
, p.ProjectValueHr
, p.ProjectValueMoney
, p.CommentPipeline
, PipelineJobStatus.CreatedTimeSTamp
, p.ModifiedTimeStamp
, Employee.Name
, Employee.Surname
, JobStatus.JobStatusName
FROM (Pipeline p 
LEFT OUTER JOIN PipelineJobStatus ON p.PipelineID = PipelineJobStatus.PipelineID)
INNER JOIN JobStatus ON (PipelineJobStatus.JobStatusID = JobStatus.JobStatusID)
LEFT OUTER JOIN Client ON (p.ClientID = Client.ClientID)
LEFT OUTER JOIN FCO ON (p.FCOID = FCO.FCOID) 
LEFT OUTER JOIN Employee ON (p.CreatedBy = Employee.EmployeeD)
WHERE PipelineJobStatus.CreatedTimeStamp IN 
   (SELECT MAX(CreatedTimeStamp) FROM PipelineJobStatus GROUP BY PipelineID)
GROUP BY p.PipelineID
, Client.ClientName
, FCO.NameFCO
, p.ProjectValueHr
, p.ProjectValueMoney
, p.CommentPipeline
, PipelineJobStatus.CreatedTimeSTamp
, p.ModifiedTimeStamp
, Employee.Name
, Employee.Surname
, JobStatus.JobStatusName

我试过LINQ:

 from pjs in db.PipelineJobStatus
 //.OrderByDescending(p=>p.CreatedTimeStamp)
 //.DistinctBy(p => p.PipelineID)
 //.FirstOrDefault()
 from pipe in db.Pipelines
    .Where(pipe => pipe.PipelineID == pjs.PipelineID).DefaultIfEmpty()
 from status in db.JobStatus
    .Where(statuses => statuses.JobStatusID == pjs.JobStatusID)
    .DefaultIfEmpty()
 from fco in db.FCOes
    .Where(fcoes => fcoes.FCOID == pipe.FCOID)
 from client in db.Clients
    .Where(clients => clients.ClientID == pipe.ClientID)
    //Where PipelineJobStatus.CreatedTimeStamp in (select max(CreatedTimeStamp) from PipelineJobStatus group by PipelineID)
    .Where(pjs.CreatedTimeStamp in select Max(pjs.CreatedTimeStamp)from PipelineJobStatus group by)
  group by { pipe.PipelineID } //group new
          by new
          {
              status.JobStatusName,
              pipe.PipelineID,
              pipe.ClientID,
              client.ClientName,
              fco.NameFCO,
              fco.FCOID,
              pipe.Employee.Name,
              pipe.Employee.Surname,
              pipe.CommentPipeline,
              pipe.CreatedBy,
              pipe.CreatedTimeStamp,
              pipe.ModifiedTimeStamp,
              pipe.ProjectValueHr,
              pipe.ProjectValueMoney
          } into g

           orderby g.Key.PipelineID descending

           select new StatusPipelineMerge
           {
               PipelineID = g.Key.PipelineID,
               ProjectValueHr = g.Key.ProjectValueHr,
               ProjectValueMoney = g.Key.ProjectValueMoney,
               CommentPipeline = g.Key.CommentPipeline,
               ClientName = g.Key.ClientName,
               FCOName = g.Key.NameFCO,
               FCOID = g.Key.FCOID,
               ClientID = g.Key.ClientID,
               CreatedTimeStamp = g.Key.CreatedTimeStamp,
               ModifiedTimeStamp = g.Key.ModifiedTimeStamp,
               CreatedBy = g.Key.CreatedBy,
               //CreatedNameSurname = g.Key.Employee.Name + " " + g.Key.Employee.Surname
               JobStatusName = g.Key.JobStatusName
           }

.Where()子句用红色加下划线..

我哪里错了?

P.S。 SQL正在运行 我会用连接来改变这个链式表,现在我只想工作。

也许商店程序在这里不是一个糟糕的选择。

0 个答案:

没有答案