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

  • Home
  • SEARCH
  • 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 3232886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:48+00:00 2026-05-17T17:12:48+00:00

How do you convert a SQL query with nested SELECT statements to a LINQ

  • 0

How do you convert a SQL query with nested SELECT statements to a LINQ statement?

I have the following SQL statement which outputs the results I need but I’m not sure how to replicate this in LINQ .

SELECT X.ITMGEDSC, (SUM(X.[ENDQTY_I]) - SUM(X.[ORDERLINES])) AS AVAIL 
FROM SELECT T1.[MANUFACTUREORDER_I],T2.[ITMGEDSC],T1.[ENDQTY_I],
(SELECT (COUNT(VW.[MANUFACTUREORDER_I]) - 1) 
FROM [PLCD].dbo.[vw_WIP_Srl] VW
WHERE VW.[MANUFACTUREORDER_I] = T1.[MANUFACTUREORDER_I]
GROUP BY VW.[MANUFACTUREORDER_I]) AS ORDERLINES 
FROM [PLCD].dbo.[vw_WIP_Srl] T1 
INNER JOIN [PLCD].dbo.IV00101 T2 ON T2.ITEMNMBR = T1.ITEMNMBR 
GROUP BY T1 [MANUFACTUREORDER_I],T2.[ITMGEDSC],T1.[ENDQTY_I]) AS X
GROUP BY X.ITMGEDSC

ITEMNMBR is the ID of an item including a revision number, for example A1008001. The last 3 numbers denote the revision. So A1008002 are the same item, just differing revisions. In my query I need to treat these as the same item and output only the quantity for the parent item number (A1008). This parent item number is the column IV00101.ITMGEDSC.

The above code would take the following data

MANUFACTUREORDER_I        ITEMNMBR      ENDQTY_I 
MAN00003140               A1048008      15 
MAN00003507               A1048008      1 
MAN00004880               A10048001     15 
MAN00004880               A10048001     15 
MAN00004880               A10048001     15 

and output the following results

ITEMNMBR      QTY 
A1048008      16 
A10048001     13*
  • The reason that this value is 13 and NOT 45 is because they are all part of the same MANUFACTUREORDER_I. In the system this therefore means that there were 15 in stock but two of these have then been transacted out of stock to be used. Hence the 3 rows, one for the goods coming into stock, the other two for two items going out of stock (ignore the quantity in these rows)

As I mentioned at the start, the SQL above gives me the output I’m after but I’m unsure how to replicate this in Linq.

UPDATE – JEFF’S ORIGINAL SOLUTION

var query = from item in db.vw_WIP_Srls
group new { item.MANUFACTUREORDER_I, item.ENDQTY_I } by item.ITEMNMBR into items 
select new
{ 
    ItemNumber = items.Key, 
    QtyAvailable = (from item in items 
           //assumes quantities equal per order number 
           group 1 by item into orders 
           select orders.Key.ENDQTY_I - (orders.Count() - 1)) 
          .Sum()
};
  • 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-17T17:12:48+00:00Added an answer on May 17, 2026 at 5:12 pm

    Here you go. Unfortunately I couldn’t see the comment you left me anymore but I believe this should be equivalent. I changed the names to match closely with your query.

    var query = from a in db.vw_WIP_Srl
                join b in db.IV00101 on a.ITEMNMBR equals b.ITEMNMBR
                group new { a.MANUFACTUREORDER_I, a.ENDQTY_I } by b.ITMGEDSC into g
                select new
                {
                    ITMGEDSC = g.Key,
                    AVAIL = (from item in g
                             group 1 by item into orders
                             select orders.Key.ENDQTY_I - (orders.Count() - 1))
                            .Sum()
                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have the following (non-straightforward) T-SQL query, which i'm trying to convert to LINQ (to
I'm unable to convert this SQL query into a working linq statement select sum(cena),
How can I convert following SQL query to LINQ in C#? I don't need
How can I convert following SQL query to LINQ in C#? I don't need
I have a SQL Server query that I need to convert to MySQL. I've
how to convert this sql query to linq query? in asp.net SELECT distinct P.Pid,P.Name,PC.categoryName,
I'm trying to convert this SQL query to LINQ: SELECT Items.p_Name Product, DiamondCategory.dc_Name Category,
I have the following SQL query: SELECT C.ID, C.Name FROM Category C JOIN Layout
I have Two SQL Query Both Return select round(convert(float,'24367.723'),2) Result:24367.72 Second: select convert(varchar(20),round(convert(float,'24367.723'),2)) Result:24367.7
I want to convert Following sql server query with Linq-to-sql query . What I

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.