LINQ
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Percentage", typeof(int));
table.Rows.Add(1, 50);
table.Rows.Add(1, 30);
table.Rows.Add(2, 0);
table.Rows.Add(2, 100);
var result = (from p in table.AsEnumerable()
group p by p["ID"] into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();
_________________________________________________
INPUT :
ID | Percentage |
1 50
1 30
2 0
2 100
OUTPUT :
ID | Percentage |
1 80
2 100
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Percentage", typeof(int));
table.Rows.Add(1, 50);
table.Rows.Add(1, 30);
table.Rows.Add(2, 0);
table.Rows.Add(2, 100);
var result = (from p in table.AsEnumerable()
group p by p["ID"] into r
select new
{
ID = r.Key,
Percentage = r.Sum((s) => decimal.Parse(s["Percentage"].ToString()))
}).ToList();
_________________________________________________
INPUT :
ID | Percentage |
1 50
1 30
2 0
2 100
OUTPUT :
ID | Percentage |
1 80
2 100
0 Comments