SQL“WHERE IN”查询转换为 LINQ

发布于 2024-12-09 01:56:30 字数 3232 浏览 0 评论 0原文

我正在尝试找到一种方法将这个非常复杂的 SQL 查询转换为 LINQ,但我似乎无法处理所有嵌入的“WHERE IN”子句。有人愿意向我伸出援助之手吗?

这是 SQL 代码(不用担心存储过程,它是对行总数的计数)

SELECT      
    (SELECT pac.Name FROM Account pac WHERE pac.AccountID = AC.ParentAccountID) AS ParentAccountName,
    ac.Name, dv.DeviceID, dv.Manufacturer, dv.Model, dv.SerialNr, dv.PrinterIPAddress,
    (SELECT TOP 1 au.AuditDate FROM PrinterAudit pa WITH (NOLOCK) INNER JOIN Audit au ON au.AuditID = pa.AuditID 
        WHERE pa.DeviceID=dv.DeviceID 
        ORDER BY AuditDate DESC) AS AuditDate,
    dbo.Get_TotalPageCountByDeviceId( DATEADD(month, -3, GETDATE()), GETDATE(), dv.DeviceID ) as TotalUsageLast3Months
FROM  Account ac WITH (NOLOCK)
          INNER JOIN Device dv ON ac.AccountID = dv.AccountID
WHERE dv.AccountID IN
          ( SELECT au.AccountID FROM Audit au WHERE au.AuditDate >= DATEADD(month, -3, GETDATE()) )
          AND (dv.Manufacturer + dv.Model) IN 
                (SELECT (dv2.Manufacturer + dv2.Model) 
                FROM Device dv2 
                WHERE dv2.AccountID = dv.AccountID 
                AND dv2.Manufacturer = dv.Manufacturer 
                AND dv2.Model = dv.Model 
                AND (dv2.ERPEquipID IS NOT NULL OR dv2.ERPData IS NOT NULL ) )
                AND dv.ERPEquipID IS NULL AND dv.ERPData IS NULL
                AND dv.DeviceID IN 
                (SELECT pa.DeviceID 
                FROM PrinterAudit pa WITH (NOLOCK) 
                INNER JOIN Audit au ON au.AuditID = pa.AuditID 
                WHERE au.AuditDate >= DATEADD(month, -3, GETDATE()))
ORDER BY ParentAccountName, ac.Name

最终结果:

var result =
    (from dv in Device
    where Audit.Any(au => au.AuditDate >= DateTime.Now.AddMonths(-3)
        && au.AccountID == dv.AccountID) 
    where Device.Any(dv2 => dv2.AccountID == dv.AccountID
        && dv2.Manufacturer == dv.Manufacturer
        && dv2.Model == dv.Model
        && (dv2.ERPEquipID != null || dv2.ERPData != null)
        && dv.ERPEquipID == null 
        && dv.ERPData == null
        && PrinterAudit.Any(pa => pa.Audit.AuditDate >= DateTime.Now.AddMonths(-3) && pa.DeviceID == dv.DeviceID))
    orderby dv.Account.ParentAccountID, dv.Account.Name
    select new
    {
        ParentAccountName = Account.Where(pac => pac.AccountID == dv.Account.ParentAccountID).Select(pac => pac.Name),
        Name = dv.Account.Name,
        DeviceID = dv.DeviceID,
        Manufacturer = dv.Manufacturer,
        Model = dv.Model,
        SerialNumber = dv.SerialNr,
        PrinterIPAddress = dv.PrinterIPAddress,
        AuditDate = (from pa in PrinterAudit where pa.DeviceID == dv.DeviceID orderby pa.Audit.AuditDate descending select pa.Audit.AuditDate).Take(1),
        TotalUsageLast3Months = (from p in PrinterAudit
                            where p.DeviceID == dv.DeviceID
                            group p by p.DeviceID into total
                            select new
                            {
                                Total = Get_TotalPageCountByDeviceId(DateTime.Now.AddMonths(-3), DateTime.Now, dv.DeviceID)
                            })

    });

I'm trying to find a way to convert this very complex SQL Query into LINQ and I can't seem to tackle all the embedded "WHERE IN" clauses. Would someone be so kind as to lend me a helping hand?

Here is the SQL code (don't worry about the stored procedure, it's a count of a row total)

SELECT      
    (SELECT pac.Name FROM Account pac WHERE pac.AccountID = AC.ParentAccountID) AS ParentAccountName,
    ac.Name, dv.DeviceID, dv.Manufacturer, dv.Model, dv.SerialNr, dv.PrinterIPAddress,
    (SELECT TOP 1 au.AuditDate FROM PrinterAudit pa WITH (NOLOCK) INNER JOIN Audit au ON au.AuditID = pa.AuditID 
        WHERE pa.DeviceID=dv.DeviceID 
        ORDER BY AuditDate DESC) AS AuditDate,
    dbo.Get_TotalPageCountByDeviceId( DATEADD(month, -3, GETDATE()), GETDATE(), dv.DeviceID ) as TotalUsageLast3Months
FROM  Account ac WITH (NOLOCK)
          INNER JOIN Device dv ON ac.AccountID = dv.AccountID
WHERE dv.AccountID IN
          ( SELECT au.AccountID FROM Audit au WHERE au.AuditDate >= DATEADD(month, -3, GETDATE()) )
          AND (dv.Manufacturer + dv.Model) IN 
                (SELECT (dv2.Manufacturer + dv2.Model) 
                FROM Device dv2 
                WHERE dv2.AccountID = dv.AccountID 
                AND dv2.Manufacturer = dv.Manufacturer 
                AND dv2.Model = dv.Model 
                AND (dv2.ERPEquipID IS NOT NULL OR dv2.ERPData IS NOT NULL ) )
                AND dv.ERPEquipID IS NULL AND dv.ERPData IS NULL
                AND dv.DeviceID IN 
                (SELECT pa.DeviceID 
                FROM PrinterAudit pa WITH (NOLOCK) 
                INNER JOIN Audit au ON au.AuditID = pa.AuditID 
                WHERE au.AuditDate >= DATEADD(month, -3, GETDATE()))
ORDER BY ParentAccountName, ac.Name

Final Result:

var result =
    (from dv in Device
    where Audit.Any(au => au.AuditDate >= DateTime.Now.AddMonths(-3)
        && au.AccountID == dv.AccountID) 
    where Device.Any(dv2 => dv2.AccountID == dv.AccountID
        && dv2.Manufacturer == dv.Manufacturer
        && dv2.Model == dv.Model
        && (dv2.ERPEquipID != null || dv2.ERPData != null)
        && dv.ERPEquipID == null 
        && dv.ERPData == null
        && PrinterAudit.Any(pa => pa.Audit.AuditDate >= DateTime.Now.AddMonths(-3) && pa.DeviceID == dv.DeviceID))
    orderby dv.Account.ParentAccountID, dv.Account.Name
    select new
    {
        ParentAccountName = Account.Where(pac => pac.AccountID == dv.Account.ParentAccountID).Select(pac => pac.Name),
        Name = dv.Account.Name,
        DeviceID = dv.DeviceID,
        Manufacturer = dv.Manufacturer,
        Model = dv.Model,
        SerialNumber = dv.SerialNr,
        PrinterIPAddress = dv.PrinterIPAddress,
        AuditDate = (from pa in PrinterAudit where pa.DeviceID == dv.DeviceID orderby pa.Audit.AuditDate descending select pa.Audit.AuditDate).Take(1),
        TotalUsageLast3Months = (from p in PrinterAudit
                            where p.DeviceID == dv.DeviceID
                            group p by p.DeviceID into total
                            select new
                            {
                                Total = Get_TotalPageCountByDeviceId(DateTime.Now.AddMonths(-3), DateTime.Now, dv.DeviceID)
                            })

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2024-12-16 01:56:30

您可以使用 Contains< 将 SQL IN 语句转换为 linq /a> 或 任何

包含

from dv in db.Device
where
  (from au in db.Audit
  where au.AuditDate >= DateTime.Now.AddMonths(-3)
  select au.AccountID).Contains(dv.AccountID)

任何

from dv in db.Device
where 
   db.Audit.Any(au => au.AuditDate >= DateTime.Now.AddMonths(-3) && 
                au.AccountID == dv.AccountID)

You convert the SQL IN statement to linq with either Contains or Any

Contains

from dv in db.Device
where
  (from au in db.Audit
  where au.AuditDate >= DateTime.Now.AddMonths(-3)
  select au.AccountID).Contains(dv.AccountID)

Any

from dv in db.Device
where 
   db.Audit.Any(au => au.AuditDate >= DateTime.Now.AddMonths(-3) && 
                au.AccountID == dv.AccountID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文