VS 2005:
Func<DataRow, decimal> filter = delegate(DataRow row)
{
if (row["dgvColumnItemId"].ToString() == itemId)
return (decimal)row["dgvColumnQty"];
return 0;
};
decimal totalQty = Enumerable.Sum<DataRow>(tripGlobal.curUserRows, filter);
or
decimal totalQty = Enumerable.Sum<DataRow>(tripGlobal.curUserRows, delegate(DataRow row)
{
if (row["dgvColumnItemId"].ToString() == itemId)
return (decimal)row["dgvColumnQty"];
return 0;
});
如果要查询DataTable.Rows的结果,视乎要把row放到一个list中.然后用list做TSource。
VS 2008:
decimal total = dataTable.AsEnumerable().Where(row => row["dgvColumnItemId"].ToString() == itemId).Sum(row => (decimal)row["dgvColumnQty"]);
decimal totalQty = (from DataRow row in dataTable.Rows
where row["dgvColumnItemId"].ToString() == itemId
select new
{
qty = row["dgvColumnQty"]
}).Sum(a => Convert.ToDecimal(a.qty));