4

this seems to work when i put an item in it to filter, but i don't want to add a filter, ie just happy to sum the column and that's it.

i've tried passing in string.empty and "" but no joy:

object objColTotal;
objColTotal = _dataSetDataTable.Compute("Sum(Price)", "");
decimal tot = Convert.ToDecimal(objColTotal);
Dkong
  • 2,748
  • 10
  • 54
  • 73

2 Answers2

5

An empty string should work.

You can always use a filter that you are certain will include all your records.


Quick test on a Decimal column:

var dt = new DataTable();
dt.Columns.Add("DecCol1", typeof(decimal));
dt.Columns.Add("StrCol1");
dt.Rows.Add(new object[] { 1.1, "r1"});
dt.Rows.Add(new object[] { 2.2, "r2" });
dt.Rows.Add(new object[] { 3.3, "r3" });

object sum = dt.Compute("SUM(DecCol1)", "");
Console.WriteLine(sum);

I get the correct answer: 6.6

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • the column is of datatype decimal. i wonder if that will cause issues or if there is a way to deal with that? – Dkong Sep 19 '11 at 04:03
  • @Dkong I don't think so; I ran a quick test and it worked for me. – Jay Riggs Sep 19 '11 at 04:11
  • thank for you help Jay. I thought I was going insane it kept bringing back zero, but it was a long list of values in the column with -ive and +ive values which totaled zero. – Dkong Sep 19 '11 at 04:22
  • @Dkong Everybody needs a sanity check now and again. Glad to help – Jay Riggs Sep 19 '11 at 04:24
0

note 2 things

  • if your column name have space you must use brackets and single or double quotation is not accepted.
  • also note that Compute returns an object which upcasting it with something (int) gives an error and you must use other methods like the samples below

samples:

int Dones = int.Parse(dt.Compute("sum(Done)", "").ToString());
int InProgresss = int.Parse(dt.Compute("sum([In Progress])", "").ToString())
Iman
  • 17,932
  • 6
  • 80
  • 90