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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:16:31+00:00 2026-05-27T06:16:31+00:00

In my database SQLServer, I´ve got a associative table ID with KID and amount

  • 0

In my database SQLServer, I´ve got a associative table ID with KID and amount. I want to sum up the values from every ID. That means that I want to have the amount from KID 6, the KID 10 etc..

My problem is, that I don´t know how to do this because there can be many values for one KID, and there can be about 100 KIDs.

I want to do that whole thing in C#. What is the best way? And what is the best way save in? Array, List?

EDIT:

I need to do it in C# with a SqlCommand because I need the values in my application!

EDIT 2:

LINQ to SQL isn´t possible because I don´t have any Entity-objects.
I want to do it with SQLReader or something like that. Is that possible?

  • 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-27T06:16:32+00:00Added an answer on May 27, 2026 at 6:16 am

    I want to do that whole thing in C#. What is the best way?

    The best way is to use in SQL Expression like this

        SELECT 
             Kid,
             SUM(AMOUNT) Sum_of_amount
        FROM
             table
        GROUP BY 
            Kid
    

    Using SQL Reader to use this SQL is pretty straight forward. Here’s an example where I’m projecting the values from the reader into a list that’s made up of an anonymous type. If you don’t want it to me anonymous you could define your own type and use that instead. You could also use a for loop instead of a linq expression.

      using (SqlConnection cn = new SqlConnection ("ConnectionString"))
            using (SqlCommand cmd = new SqlCommand (@"
                    SELECT 
                         Kid,
                         SUM(AMOUNT) Sum_of_amount
                    FROM
                         table
                    GROUP BY 
                        Kid", cn))
    
            {
                cn.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                { 
                var list = (from r in dr.Cast<DbDataRecord>()
                            select new {Id= r.GetInt32(0), SumOfAmount = r.GetInt32(1) }).ToList();
                }
    
            }
    

    If you’re using something like Linq to SQL its pretty straight forward to do it like this

    Table<Kids> kids = db.GetKids();
    var kidsSum =  
            (from k in kids
            group p by p.Id into g 
            select new {ID = g.Key, SumOfAmount = g.Sum(p => p.Amount) }).ToList(); 
    

    And what is the best way save in? Array, List?

    There is no “best” way but directly manipulated arrays has problems. You’re better off with the Generic Collection of your choosing.

    Typed dataset are also a valid choice although many people don’t like them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created dataset that contains Table View from SqlServer 2008 database. Then I created
I've got a SQLServer Database and an application (.NET) that configures data on that
I have a SqlServer database that I've manually filled with some test data. Now
I have a 3 table SQLServer Database. Project ProjectID ProjectName Thing ThingID ThingName ProjectThingLink
I've got a legacy database that I'm connecting to via the sqlserver adapter. One
I've got a database from SQL Server 2005 that I am programmatically attaching to
I have got SQL Server database in which Table column name have spaces. For
I've got a rather large SQL Server 2005 database that is under constant development.
I've got a large table in a SQL Server 2005 database and I'd like
We've got a product that uses a Microsoft SQL Server database. We've got customers

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.