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! 2 57291
"Ronald S. Cook" <rc***@westinis .comwrote in message
news:B2******** *************** ***********@mic rosoft.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.
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.A dd("Id", typeof(int));
table.Columns.A dd("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.AsEnumera ble()
group row by row.Field<int>( "Id") into grp
orderby grp.Key
select new
{
Id = grp.Key,
Sum = grp.Sum(r =r.Field<decima l>("Value"))
};
foreach (var grp in query)
{
Console.WriteLi ne("{0}\t{1}", grp.Id, grp.Sum);
}
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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/1/2004 4000.00 40.00 4040.00
1/2/2004 1000.00 10.00 1010.00
1/2/2204 2000.00 20.00 2020.00
1/3/2004 1500.00 15.00 1515.00
I want my resultant DataTable to show in DataGrid as
|
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?
Bart
|
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 best way to do it?
Currently, I am sorting them and reading each row and comparing
current row's col1 and col2 with previous row's col1 and col2 and
keeping the total in an arraylist. But this is very slow; I am pretty
new to C#, any help is...
|
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 for all your inputs.
|
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 datacolumns do not trigger
their expression when other columns from which the expressions are derived
are updated. Below is a basic example of what I am doing.
User enters values into an asp.net form and clicks a button.
Retrieve dataset from...
| |
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 and use a
StreamWriter to build the strings. However, the problem lies with the
reading of each row.
Most of the test takers don't give answers to ALL of the items in a
test, but those unanswered items need to be accounted for by using a
comma...
|
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 a line item
where you list the attribute with 0 as the count.
|
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 shown is first code. By clicking the proceed button on the first form I can follow the GBL_ID variable to the second form in debug mode.
While hitting F8 I can follow the process until a I get an error message:
Object variable or with block...
|
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)
{
string Crefeicao2 = LinhaDetalhe.ToString();
string CFAmiliaPrato2 = LinhaDetalhe.ToString();
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |