Click here to Skip to main content
13,194,662 members (52,006 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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();
        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(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 21:13pm
Updated 26-Feb-16 2:29am
Rate this: bad
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.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.
AshiqueAhammed 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 22-Sep-10 5:44am
You could use '' to having LINQ in VS2005.
Rate this: bad
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'
Rate this: bad
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'
object sum1 = dTable.Compute("SUM(AutoID)", "Address = 'Ram Nagar, India'");

you can use all SQL boolean command such as LIKE, <, >, <>

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171018.2 | Last Updated 26 Feb 2016
Copyright © CodeProject, 1999-2017
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