Saturday, 27 August 2011

Converting Datatable into List

attend.getData(); //function call to get record in dataset

var l = (from x in _attend.AttendanceDS.Tables["EmpAttendanceSummary"].AsEnumerable()
  select new AttendanceSummary
  {
      EmpId = x.Field("EMPLOYEE_ID"),
      TotalHours = String.IsNullOrEmpty(x.Field("TotalWorkingHours")) 
      ? 0 :Convert.ToInt64(x.Field("TotalWorkingHours")),
      Department = x.Field("department_id"),
      Designation = x.Field("designation_id"),
      }).ToList();

//now you can get values from list by class object

AttendanceSummary _o = new AttendanceSummary();

       _o. HighestHours = l.Max(p => p.TotalHours);
       _o. DepartmentHighestHours = l.Where(p => p.TotalHours > 0 && 
       p.Department == int.Parse(5001)).Max(p => p.TotalHours);
      
       return _o;

public class AttendanceSummary
{
  public Int64 EmpId { get; set; }
  public Int64 TotalHours { get; set; }
  public int Department { get; set; }
  public int Designation { get; set; }
  public long HighestHours { get; set; }
  public long DepartmentHighestHours { get; set; }
 }

No comments:

Post a Comment