如何将SQL脚本转换为Linq

时间:2014-02-26 10:29:11

标签: sql linq count group-by

我有一个我无法翻译成linq的sql脚本。愿你们中的一个人能帮助我找到正确的方向。

我的重大问题是伯爵和小组:

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

1 个答案:

答案 0 :(得分: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();