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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:41:49+00:00 2026-05-14T00:41:49+00:00

I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as

  • 0

I have a simple SQL query:

Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as Units
FROM tbl_Results
GROUP BY ID, OptionName

The results I got were:

'1' '4' 'Approved'    '40' 
'2' '1' 'Rejected'    '19'
'3' '2' 'Not Decided' '12'

I have to encrypt the data in the database, and as such am unable to sum the data in relational form. To get around this I decrypt the data in the application layer, and want to use LINQ to query it there. I need the following results:

'1' 'Approved'     '10'
'3' 'Not Deceided' '6'
'2' 'Rejected'     '19'
'1' 'Approved'     '15'
'1' 'Approved'     '5'
'3' 'Not Deceided' '6'
'1' 'Approved'     '10'

I put these results into class and create a strongly typed list:

public class results
{
 public int ID {get;set;}
 public string OptionName {get;set;}
 public int Unit {get;set;}
}

I almost have the LINQ query to bring back the results like the SQL query about:

var q = from r in Results
        group p.Unit by p.ID
        int g
        select new {ID = g.Key,
                    Selections = g.Count(),
                    Units = g.Sum()};

How do I ensure my LINQ query also give me the Option Name?

If I created a class called Statistics to hold my results how would I modify the LINQ query to give me List<Statistics> result set?

public class results
    {
     public int ID {get;set;}
     public int NumberOfSelections { get; set; }
     public string OptionName {get;set;}
     public int UnitTotal {get;set;}
    }
  • 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-14T00:41:50+00:00Added an answer on May 14, 2026 at 12:41 am

    You’re currently just grouping by the ID. Instead, as per the original query, you need to group by the option name too. That way each group’s key will contain both the ID and the option name. Here’s just that change:

    var q = from r in Results
            group r.Unit by new { p.ID, p.OptionName } into g
            select new { ID = g.Key.ID,
                         OptionName = g.Key.OptionName
                         Selections = g.Count(),
                         UnitTotal = g.Sum() };
    

    Now, it wasn’t immediately obvious to me that you were selecting just the “unit” part for the group item. I missed “group p.Unit by” in the query expression. My bad… but others may do likewise. Here’s an alternative, which makes the group contain the items, and then sums the units in the projection:

    var q = from r in Results
            group r by new { p.ID, p.OptionName } into g
            select new { ID = g.Key.ID,
                         OptionName = g.Key.OptionName
                         Selections = g.Count(),
                         UnitTotal = g.Sum(x => x.Unit) };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 337k
  • Answers 337k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hopefully I have grasped what you are trying to do.… May 14, 2026 at 4:10 am
  • Editorial Team
    Editorial Team added an answer There is no functional difference. It's a question of convenience… May 14, 2026 at 4:10 am
  • Editorial Team
    Editorial Team added an answer Use PackageManager to find out the installed applications and such.… May 14, 2026 at 4:10 am

Related Questions

I am fairly new to Linq To SQL but trying to run what should
I'm using Oracle, and I have a very large table. I need to check
I have a basic Customer/Order/OrderItem/Product object graph. Customer has Many Orders, Order has Many
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
After watching, the latest RailsCasts with the include and joins information, I know I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.