Click here to Skip to main content
14,301,556 members
Rate this:
Please Sign up or sign in to vote.
See more:
protected void btnmakepmnt_click(object sender, EventArgs e)
{
DataTable dtpymnt = (DataTable)Session["checkdata"];

      if (rdio_rps.Checked == true)
      {

          var sum = dtpymnt.AsEnumerable().Sum(dr => dr.Field<int>("SevicePriceRup"));
          Label2.Text = sum.ToString();
}
}

// here datatable return 3 column with records, i want to sum values of column "SevicePriceRup", please check the code or provide me other ways to resolve.
here "sevicePriceRup" has datatype bigint
Posted
Updated 15-Jan-16 20:24pm
v6
Comments
Member 12175752 16-Jan-16 2:04am
   
here "sevicePriceRup" has datatype bigint
Rate this:
Please Sign up or sign in to vote.

Solution 2

With big integer, one way is to calculate the sum by hand. It's not 'pretty' but it works. Consider the following example:
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("Col1", typeof(System.Numerics.BigInteger));
table.Rows.Add(5);
table.Rows.Add(7);

System.Numerics.BigInteger sum = 0;
foreach (System.Data.DataRow dr in table.Rows)
{
   sum = System.Numerics.BigInteger.Add(sum, dr.Field<System.Numerics.BigInteger>("Col1"));
}

If you need the calculation in different places, you can create for example an extension method to ease the coding.
public static class BI
{
   public static System.Numerics.BigInteger SumBigIntCol(this System.Data.DataTable table, string columnName)
   {
      System.Numerics.BigInteger sum = 0;
      foreach (System.Data.DataRow dr in table.Rows)
      {
         sum = System.Numerics.BigInteger.Add(sum, dr.Field<System.Numerics.BigInteger>(columnName));
      }

      return sum;
   }
}

And to use would look like
System.Numerics.BigInteger calcresult = table.SumBigIntCol("Col1");
   
Comments
Richard Deeming 16-Jan-16 6:25am
   
NB: SQL's bigint maps to .NET's Int64 type, not BigInteger. AFAIK, SQL doesn't have an equivalent for BigInteger.
Wendelius 16-Jan-16 6:29am
   
That's a good point. If the data comes from SQL Server and from a bigint column then Int64 would be the correct choice.
Rate this:
Please Sign up or sign in to vote.

Solution 3

One way to approach this is to define extension methods:
public static class DataTableExtensions
{
    public static int SumColumnIntByIndex(this DataTable table, int id)
    {
        return table.AsEnumerable().Sum(x => x.Field<int>(id));
    }

    public static decimal SumColumnDecByIndex(this DataTable table, int id)
    {
        return table.AsEnumerable().Sum(x => x.Field<decimal>(id));
    }

    public static int SumColumnIntByID(this DataTable table, string id)
    {
        return table.AsEnumerable().Sum(x => x.Field<int>(id));
    }

    public static decimal SumColumnDecByID(this DataTable table, string id)
    {
        return table.AsEnumerable().Sum(x => x.Field<decimal>(id));
    }
}
Sample use: assuming you have a valid DataTable, "dt," with a Decimal Field at Column Index #3, and Column #3 is named "Salary:"
decimal total1 = dt.SumColumnDecByIndex(3);
decimal total2 = dt.SumColumnDecByID("Salary");
You can easily add similar extensions for other Types (long, double, etc.). However note that as of .NET 5.0 IEnumerable.Sum can be used with only these Types:
double,double?
int,int?
decimal,decimal?
long,long?
float,float?
While you could do some re-factoring and make an extension that handles multiple Types, I personally believe that in writing Extensions the "DRY" principle ("don't repeat yourself") should be ignored, and that it's better to write separate Extensions ... which also gets you out of the problem of how to return the right Type you'll have with a generic Extension.
   
v6
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try the below code

protected void btnmakepmnt_click(object sender, EventArgs e)
{
      DataTable dtpymnt = (DataTable)Session["checkdata"];
      if (rdio_rps.Checked == true)
      {
          object sumObject;
          sumObject = dtpymnt.Compute("Sum(SevicePriceRup)", "");
          Label2.Text = sumObject .ToString();
      }
}
   
Comments
Member 12175752 16-Jan-16 2:34am
   
Hi, salman
Now m getting error "Invaild usage aggregate function sum() and Type String.
at
sumObject = dtpymnt.Compute("Sum(SevicePriceRup)", "");// this line
Salman622 21-Jan-16 8:14am
   
try out
SumObject = dtpymnt.Compute("Sum(SevicePriceRup)", "[SevicePriceRup] IS NOT NULL");
Rate this:
Please Sign up or sign in to vote.

Solution 4

SQL's bigint type maps to .NET's Int64 type. You're trying to cast the column to Int32 to compute the sum, which won't work.

Change the code to sum the column using the correct type:
var sum = dtpymnt.AsEnumerable().Sum(dr => dr.Field<long>("SevicePriceRup"));
   

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



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