I want to sum the value of a certain column on my datatable
, where column x is equal to a value AND column y is equal to another value. I can implement this when I just filter on column X as follows:
dt.Compute("Sum([Cost])", "[COLUMNX] = 'blah'");
This works fine, however I now want to add another column to the filter, so something like dt.Compute("Sum([Cost])", "[COLUMNX] = 'blah'", "[COLUMNY] = 'blah'");
How can I filter on two different columns please.
You can use AND
as explained here
dt.Compute("Sum([Cost])", "[COLUMNX] = 'blah' AND [COLUMNY] = 'blah'");
or with LInq-To-DataTable which has no limits:
double sumCost = dt.AsEnumerable()
.Where(r => r.Field<string>("COLUMNX")=="blah" && r.Field<string>("COLUMNY")=="blah")
.Sum(r => r.Field<double>("Cost"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With