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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:07:09+00:00 2026-06-10T21:07:09+00:00

Please help me to get my head around querying using LINQ with a GROUP

  • 0

Please help me to get my head around querying using LINQ with a GROUP and SUM.

// Query the database
IEnumerable<BestSeller> best_sellers = from bs in (db.MYDATABASE).Take(25)
                                       where bs.COMPANY == "MY COMPANY"
                                       group bs by bs.PRODCODE into g
                                       orderby g.Sum(g.MQTY)
                                       select new BestSeller()
                                       {
                                           product_code = ,
                                           product_description = ,
                                           total_quantity =  
                                      };

I wish to:

  • Take the top 25 items from db.MYDATABASE
  • Group all the results by bs.PRODCODE
  • Order it by the sum total for each bs.PRODCODE
  • Where the company is “MY COMPANY”
  • Then pipe the data in to my BestSeller() objects

I’m confused, because as soon as I add my group in to the mix, my bs variable becomes useless.

  • 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-10T21:07:10+00:00Added an answer on June 10, 2026 at 9:07 pm

    I’m confused, because as soon as I add my group in to the mix, my bs variable becomes useless.

    Yes, because you no longer have a single item – you’re now processing a sequence of groups of items. You can get at first item for each group, which I assume would be a valid way of getting at the description?

    var query =  from bs in db.MYDATABASE.Take(25)
                 where bs.COMPANY == "MY COMPANY"
                 group bs by bs.PRODCODE into g
                 orderby g.Sum(x => x.MQTY)
                 select new BestSeller
                 {
                     product_code = g.Key,
                     product_description = g.First().DESCRIPTION,
                     total_quantity = g.Sum(x => x.MQTY) 
                 };
    

    Note that without specifying an ordering, “the top 25 items from db.MYDATABASE” makes no sense. “Top” in what way? You may well want:

    from bs in db.MYDATABASE.OrderByDescending(x => x.Price).Take(25)
    

    or something similar. Note that if none of those have a company of “MY COMPANY” you’ll end up with no results…

    Or if you want the top 25 bestsellers, you want the “take” part at the very end:

    var query =  from bs in db.MYDATABASE
                 where bs.COMPANY == "MY COMPANY"
                 group bs by bs.PRODCODE into g
                 orderby g.Sum(x => x.MQTY) descending
                 select new BestSeller
                 {
                     product_code = g.Key,
                     product_description = g.First().DESCRIPTION,
                     total_quantity = g.Sum(x => x.MQTY) 
                 };
    var top25 = query.Take(25);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please help me get my head around this bug? With Firefox its
I can't quite get my head around this idea, wonder if someone could help
I just cant get my head around regex, any help appreciated ! I have
I'm struggling to get my head around LINQ and have come to the conclusion
Could somebody please help me get Clang up and running? (I don't have 3.2)
Could you please help me to get image upload working on a view with
Please help with the following error that I get with Spring Security 3.05 and
Please help me. I can't figure out why I get a core dumped when
Could someone please help explain why I can't get this to work? I properly
Im a beginner to css, please can someone help me get this element to

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.