'Linq query on Datatable object
I need to perform following query on a datatable.But it not getting data from the datatable object.It only shows zeros when wrote to an another table.Can Linq querys perform on datatables?
LINQ query:
object sumObject1;
object sumObject2;
sumObject1 = dataTable.AsEnumerable().Where(r => r["Type"]?.ToString() == "Income" && dates.Contains((DateTime)r["Date"]))
.Sum(rs => (decimal)rs["Amount"]);
sumObject2 = dataTable.AsEnumerable().Where(r => r["Type"]?.ToString() == "Expense" && dates.Contains((DateTime)r["Date"]))
.Sum(rs => (decimal)rs["Amount"]);
Datatable object as follows:
Datatable:
DataTable dt = new DataTable
{
Columns = {
{ "Total Incomes",typeof(int) },
{ "Total Expenses",typeof(int) },
{ "Balance",typeof(int) },
{ "Week",typeof(string) }
},
TableName = "Reports"
};
Writing to datatable:
var row = dt.NewRow();
row["Week"] = week;
row["Total Incomes"] = Convert.ToInt32(sumObject1);
row["Total Expenses"] = Convert.ToInt32(sumObject2);
row["Balance"]= Convert.ToInt32( balance);
dt.Rows.Add(row);
dt.AcceptChanges();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

