c# - Sum values in datatable using linq based on conditions -
i'm having datatable mentioned below.
id percentage 1 50 1 30 2 0 2 100
result:
id percentage 1 80 2 100
i tried , doesn't work
var n = dt.asenumerable() .where(r => (int)r["id"] != "0" || (int)r["id"] != "100") .sum(r => (int)r["percentage"]);
i'm new linq , pls provide suggestions.
now need sum percentage each id , percentage each id should 0 or 100 percentage.
if 1 of id in table doesn't have 0 or 100 need alert. pls suggest me how can in linq , think best way.
var result = row in dt.asenumerable() group row row["id"] g select new { id = g.key, sum = g.sum(x => int.parse(x["percentage"].tostring())) }; var erroritems = result.where(x => x.sum != 100 && x.sum != 0); if (erroritems.any()) { var ids = erroritems.select(x => x.id); string msg = string.format("id(s): [{0}] don't meet condition.", string.join(",", ids)); messagebox.show(msg); }