473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How group by and sum values in DataTable?

I have a DataTable that looks like this:

Id Value
123 4.0
123 5.0
234 1.0
345 2.0
345 3.0

I want to end up with (probably a new DataTable) that contains the sum of
values grouped by Id like this:

Id SumOfValue
123 9.0
234 1.0
345 5.0

Anyone know how to write this?

Thanks!
Jun 27 '08 #1
2 57096
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:B2**********************************@microsof t.com...
>I have a DataTable that looks like this:

Id Value
123 4.0
123 5.0
234 1.0
345 2.0
345 3.0

I want to end up with (probably a new DataTable) that contains the sum of
values grouped by Id like this:

Id SumOfValue
123 9.0
234 1.0
345 5.0

Anyone know how to write this?
There is no standard functionality in the DataTable itself to do this. If
you can use LINQ, then the groupby clause will do the aggregation for you.
Otherwise, you will have to sort the DataTable on the Id column, and then
loop through the rows adding the Value column until the Id changes, and then
dump the current Id and accumulated value onto the new table and repeat
until all the rows have been processed.

Jun 27 '08 #2
To show the LINQ approach (note most of this code is setting up the
table to demo with):

using System;
using System.Data;
using System.Linq;

class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Value", typeof(decimal));
table.Rows.Add(123, 4.0M);
table.Rows.Add(123, 5.0M);
table.Rows.Add(234, 1.0M);
table.Rows.Add(345, 2.0M);
table.Rows.Add(345, 3.0M);

var query = from row in table.AsEnumerable()
group row by row.Field<int>("Id") into grp
orderby grp.Key
select new
{
Id = grp.Key,
Sum = grp.Sum(r =r.Field<decimal>("Value"))
};

foreach (var grp in query)
{
Console.WriteLine("{0}\t{1}", grp.Id, grp.Sum);
}
}
}
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
1
by: AA Arens | last post by:
I created an Option Group (go to Design View > Toolbox) and want the values to be filled in a present table. How to make it possible that not only nummeric values can be given, but also names? ...
4
by: luke | last post by:
Hello, I have a dataview that contains about 300k records with 3 cols (read in from a directory). I need to get the count of rows by col1 and col2 (grouping by col1 and col2). What would be the...
4
by: | last post by:
I have a two dimensional array. The first dimension is a number, and the second dimension is a string. Given the second dimension's value (String), how do I search for the first dimension? Thanks...
0
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these...
2
by: ILCSP | last post by:
Hi, I have a sql table containing the answers for some tests. The information in this table is presented vertically and I need to create strings with them. I know how to read the data in VB.Net...
1
by: sammy | last post by:
If you have a select with 2 attributes where you group by one attribute and do a count() for the second attribute, if the count() is 0 then that row is never displayed. How would you instead see...
0
by: Ample | last post by:
This is what I'm trying to do. I created a form for a user to enter a name. The information is saved in SQL DB employees table. The employees table auto number is assign to global variable GBL_ID as...
2
by: JoaquimC | last post by:
Hello, I need to delete a group of rows from a DataTable based on a condition. Something like: string CRefeicao = "01", CFamiliaPrato = "02"; foreach (DataRow LinhaDetalhe in Detalhe.Rows)...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.