Home > Software engineering >  How can I use Datatable.compute() with list of data
How can I use Datatable.compute() with list of data

Time:03-14

I want to add values within list of dates using datatable.compute.

List of dates as follows

var dates = Enumerable.Range(0, 7).Select(days => monday.AddDays(days)).ToList();

And I want compute the values in those days.I don't know how to use filter with the list. I've tried following method but no luck.

sumObject1 = table.Compute("Sum(Amount)", "Type='Income' AND Date = dates");

Rest of the code works fine.

CodePudding user response:

You can use LINQ for that:

 table.AsEnumerable().Where(r => r["Type"]?.ToString() == "Income" && dates.Contains((DateTime)r["Date"]))
.Sum(rs => (decimal)rs["Amount"]);

You won't be able to use the list with the DataTable expressions.

  • Related