Sunday, November 1, 2009

DataGridView - Sum of a Column using LINQ - Windows Forms

You can use this one liner using LINQ instead of looping throw DataGridView Rows.

DataView view = adventureWorksDataSet.Tables[0].DefaultView ;
// view.RowFilter = "Included = true";
// LINQ Sum: 152.1
decimal total = view.Cast().Sum(row => (Int32)row["EmployeeID"]);
// LINQ count: 2 (to prove we have exluded the one row)
int count = view.Cast().Count();

No comments:

Post a Comment