David Smith

David Smith

  • NA
  • 2k
  • 0

Linq Clean up

Dec 20 2015 10:22 PM
The logic is working and correct. I want to know if someone could help me write this in a clean robust way. I want short the lines of code if possible 
 
private DataTable AdjustTimesheetHours()
{
ColorDataSet.Tables[PrimaryTable].Columns.Add("adjustedHours");
foreach (var datarow in ColorDataSet.Tables[SummaryTable].AsEnumerable())
{
         string empno = datarow.Field<string>("empno");
            double diffHours = datarow.Field<double>("diffHours");
               var query = ColorDataSet.Tables[PrimaryTable].AsEnumerable().Where(s => s.Field<string>("empno")
                                                                                                                                                            == empno && s.Field<string>("task")
                                                                                                                                                               == "Regular" && s.Field<double>("hrs") >= diffHours)
                                                                        .OrderByDescending(s => s.Field<double>("hrs"))
                                                                        .FirstOrDefault();
double adjustedHours = 0;
if (diffHours > 0)
{
if (query.Field<double>("hrs") >= diffHours)
{
            double hours = query.Field<double>("hrs");
            string project = query.Field<string>("project");
            string task = query.Field<string>("task");
            adjustedHours = hours - diffHours;
               //Update datatable with adjusted hours
               ColorDataSet.Tables[PrimaryTable].AsEnumerable().Where(row => row.Field<string>("empno") == empno &&                                                                                                                                                       row.Field<string>("project")
                                                                                                                                                         == project && row.Field<double>("hrs")
                                                                                                                                                      == hours && row.Field<string>("task") == task)
                                                                     .Select(b => b["adjustedHours"] = adjustedHours)
                                                                        .ToList();
                     }
            }
      }
            return ColorDataSet.Tables[PrimaryTable];
}