Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
4.78/5 (2 votes)
See more:
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:
C#
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
Updated 26-Feb-16 2:29am
v2

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.


C#
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.
 
Share this answer
 
Comments
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 'http://aspalliance.com/859' to having LINQ in VS2005.
Try wrapping Address in single quotes - "group by " + "'" "Address" + "'".

In a sql query Adress should be in a single quote - 'Address'
 
Share this answer
 
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, <, >, <>
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900