2014-02-26 112 views
2

我確實有一個我不能轉換成linq的sql腳本。願你們中的一人能幫助我找到正確的方向。如何將SQL腳本轉換爲Linq

我的大問題是數量和由羣:

SELECT 
    wfs.ServerId, 
    COUNT(wfss.Name) AS Records, 
    wfs.DiskId 
FROM WorkflowStep wfs 
INNER JOIN WorkflowStepStatus wfss ON wfs.WorkflowStepStatusId = wfss.Id 
WHERE wfs.WorkflowId = (
          SELECT 
           Id 
          FROM Workflow wf 
          WHERE wf.Name = 'Collecting data virutal' 
         ) 
AND wfs.StepNumber IN (1, 2, 3) 
AND wfss.Name = 'Processed' 
GROUP BY wfs.ServerId, 
      wfss.Name, 
      wfs.DiskId 
+0

你能告訴類模型,那麼導航屬性和關聯的多重性是可見的?另外,請告訴這應該是哪種LINQ味道。你試過Linqer了嗎? –

+0

你不能比較'wfs.WorkflowId'和'(SELECT Id FROM Workflow wf WHERE wf.Name ='Collecting data virutal')''你需要使用'IN'運算符,就像'wfs.WorkflowId IN(SELECT Id從工作流wf WHERE wf.Name ='收集數據virutal')',我說得對嗎? –

回答

1

這應該工作

var result = wfs.Join(wfss, 
    t => t.WorkflowStepStatusId, 
    u => u.ID, 
    (t, u) => new { 
     t.ServerID, 
     t.WorkflowId, 
     t.StepNumber, 
     u.Name, 
     t.DiskID 
    }) 
    .Where(t => t.WorkflowId == wf.FirstOrDefault(u => u.Name == "Collecting data virutal").ID && 
       t.Name == "Processed" && 
       new List<int> { 1, 2, 3 }.ToArray().Contains(t.StepNumber)) 
    .GroupBy(t => new { t.ServerID, t.Name, t.DiskID }) 
    .Select(t => new { 
     t.Key.ServerID, 
     Records = t.Key.Name.Count(), 
     t.Key.DiskID 
    }) 
    .ToList(); 
+0

一個簡單的更正:'t.Key.Name'沒有'Count'方法,而不是't.Key.Name.Count()',你可以試試'Records = t.Count()' –