Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6669461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:09:09+00:00 2026-05-26T03:09:09+00:00

I’m trying to find a way to convert this very complex SQL Query into

  • 0

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)
                            })

    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:09:10+00:00Added an answer on May 26, 2026 at 3:09 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.