@Mohammad Nadeem Alam, Welcome to Microsoft Q&A, you could try to group your datatable by the EmployeeName and get the sum of every Employee's Totoal Hours Earned.
Here is a code example you could refer to.
DataTable table = new DataTable();
table.Columns.Add("EmployeeID", typeof(int));
table.Columns.Add("EmployeeName", typeof(string));
table.Columns.Add("CostCenterCode", typeof(int));
table.Columns.Add("TotalHoursWorked", typeof(decimal));
table.Columns.Add("TotalHoursEarned", typeof(decimal));
table.Rows.Add(901234,"Mark",141005,41.5,42.25);
table.Rows.Add(901367, "Julius", 141005, 0.5, 0.75);
table.Rows.Add(901675, "Brutus", 1410013,15.45, 17.325);
table.Rows.Add(901675, "Brutus", 1410013, 36.05, 40.425);
table.Rows.Add(901675, "Steve", 1410013, 14.7, 15.45);
table.Rows.Add(901675, "Steve", 1410013, 34.3, 36.05);
var result = from row in table.AsEnumerable()
group row by row.Field<string>("EmployeeName") into m
select new
{
EmployeeName=m.Key,
TotalHoursEarned = m.Select(s => s.Field<decimal>("TotalHoursEarned")).Sum()
};
table.Columns.Add("Allocation", typeof(int));
foreach (DataRow item in table.Rows)
{
foreach (var key in result)
{
if (item["EmployeeName"].ToString()==key.EmployeeName)
{
item["Allocation"] = (Convert.ToDecimal(item["TotalHoursEarned"])/key.TotalHoursEarned) * 100;
}
}
}
Result:
Hope my code could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.