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?
The best way is to use in SQL Expression like this
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
forloop instead of a linq expression.If you’re using something like Linq to SQL its pretty straight forward to do it like this
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.