Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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

With big integer, one way is to calculate the sum by hand. It's not 'pretty' but it works. Consider the following example:
C#
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.
C#
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
C#
System.Numerics.BigInteger calcresult = table.SumBigIntCol("Col1");
 
Share this answer
 
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.
One way to approach this is to define extension methods:
C#
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:"
C#
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.
 
Share this answer
 
v6
Try the below code

C#
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();
      }
}
 
Share this answer
 
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");
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:
C#
var sum = dtpymnt.AsEnumerable().Sum(dr => dr.Field<long>("SevicePriceRup"));
 
Share this answer
 

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