Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Friends..
 
I need a help - here is my problem:
I have a Data Table and i am using the Compute method to find the sum
It looks like this:
DataTable dTable = new DataTable();
        dTable.Columns.Add("AutoID");
        dTable.Columns.Add("Name");
        dTable.Columns.Add("Address");
        DataRow row = null;
        for (int i = 0; i < 5; i++)
        {
            row = dTable.NewRow();
            row["AutoID"] = i + 1;
            row["Name"] = i + " - Ram";
            row["Address"] = "Ram Nagar, India";
            dTable.Rows.Add(row);
        }
        dTable.Rows.Add(6, "Manual Data - 1", "Manual Address - 1, USA");
        dTable.Rows.Add(7, "Manual Data - 2", "Manual Address - 2, USA");
 
        double sum = Convert.ToDouble(dTable.Compute("SUM(AutoID)", ""));
 

//Upto here its fine...

        object sum1 = dTable.Compute("SUM(AutoID)", "group by Address");
 //this makes error like 
//Syntax error: Missing operand after 'by' operator.
 
Any help.....
Posted 21-Sep-10 22:13pm
Edited 22-Sep-10 0:21am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The data table itself will not provide you with group by operation some extent it will be better if you use LINQ on Data table to accomplish your solution.
 
Look at this Sample.
 

DataTable dTable = new DataTable();
        dTable.Columns.Add("id",typeof(int));
        dTable.Columns.Add("name",typeof(string));
        dTable.Rows.Add(1, "hiren");
        dTable.Rows.Add(2, "solanki");
        dTable.Rows.Add(3, "hiren");
        var query = from row in dTable.AsEnumerable()
                group row by row.Field<string>("name") into grp
                select new
                {
                    Id = grp.Key,
                    sum = grp.Sum(r=>r.Field<int>("id"))
                };
        foreach (var grp in query)
        {
            Response.Write(String.Format("The Sum of '{0}' is {1}", grp.Id, grp.sum));
        }
 
Please Vote or Mark as A answer if Solved.
  Permalink  
Comments
AshiqueAhammed at 22-Sep-10 5:38am
   
Thanks my friend...
 
can u help me to do it in VS 2005..
because i am using VS 2005 ..
Hiren Solanki at 22-Sep-10 5:44am
   
You could use 'http://aspalliance.com/859' to having LINQ in VS2005.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try wrapping Address in single quotes - "group by " + "'" "Address" + "'".
 
In a sql query Adress should be in a single quote - 'Address'
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Do you want to get a table with
  object sum1 = dTable.Compute("SUM(AutoID)", "group by Address");
?
 
It's wrong, Compute return a single value.
 
and the second parameter is the filter
so for select SUM(AutoId) from mytable where address = 'Ram Nagar, India'
do
 object sum1 = dTable.Compute("SUM(AutoID)", "Address = 'Ram Nagar, India'");
you can use all SQL boolean command such as LIKE, <, >, <>
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100