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 8162425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:42:19+00:00 2026-06-06T18:42:19+00:00

I have a Linq expression that (for northwind) gets the total qty ordered per

  • 0

I have a Linq expression that (for northwind) gets the total qty ordered per product for customer id ‘ALFKI’

from od in db.OrderDetails
    where od.Order.CustomerID == "ALFKI"
    group od by od.Product.ProductName into g1
    select new { ProductName = g1.Key, Total = g1.Sum(od => od.Quantity) }

This is fine but to fully understand Linq, I want to try and formulate the expression in a world where Linq2Sql doesn’t nicely build the property bridges through foreign keys.

For instance, in the above expression, I’m accessing od.Order.CustomerID. I want to assume that od.OrderID is as far as I can go.

Looking at the SQL for the expression, I have:

SELECT SUM(CONVERT(Int,[t0].[Quantity])) AS [Total], [t2].[ProductName]
FROM [Order Details] AS [t0]
INNER JOIN [Orders] AS [t1] ON [t1].[OrderID] = [t0].[OrderID]
INNER JOIN [Products] AS [t2] ON [t2].[ProductID] = [t0].[ProductID]
WHERE [t1].[CustomerID] = @p0
GROUP BY [t2].[ProductName] 

This is as far as I’ve managed to get:

from od in db.OrderDetails
    join o in db.Orders on od.OrderID equals o.OrderID
    join p in db.Products on od.ProductID equals p.ProductID
    where o.CustomerID == "ALFKI"
    group od by od.ProductID into g1
    select new { ProductName = g1.Key, Total = g1.Sum(od => od.Quantity) }

This is almost there but g1.Key is referring to the ProductID. I can’t seem to get at the ProductName and the order quantity at the same time.

Thanks

  • 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-06-06T18:42:21+00:00Added an answer on June 6, 2026 at 6:42 pm

    Try creating a new anonymous type for your group by:

    from od in db.OrderDetails
        join o in db.Orders on od.OrderID equals o.OrderID
        join p in db.Products on od.ProductID equals p.ProductID
    where o.CustomerID == "ALFKI"
    group od by new { p.ProductID, p.ProductName } into g1
    select new { 
        ProductName = g1.Key.ProductName, 
        Total = g1.Sum(od => od.Quantity) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq provider that sucessfully goes and gets data from my chosen
I have a LINQ expression that groups customers from an Azure Table Storage by
I have a LINQ expression that gets used as a filer in a LINQ
I have the following LINQ expression that's not returning the appropriate response var query
I have the following linq expression pulling all data from my database: var items
I have a linq expression that returns transactions in groups. Each transaction has a
In F# I have a function that returns System.Linq.Expression instances: and System.Object with member
I am creating Linq expression trees from F# that operates on a custom datatype
I have a Linq expression that operates on a list of objects for which
I have a linq expression that I've been playing with in LINQPad and 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.