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 21:13pm
Edited 21-Sep-10 23:21pm
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  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 22 Sep 2010
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