Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table in which i am storing multiple record of same employee and there are multiple employees also,i want to get list last record of all employees,then base on their status divide it like on job empstatus_Id 1,absent empstatus_Id 2, leave empstatus_Id 3,i want to get list of all employees latest record.
SQL
[Id]
      ,[Employee_Id]
      ,[Rank_Id]
      ,[EmployeeStatusType_Id]
      ,[Sector_Id]
      ,[DutyType_Id]
      ,[EmployeeShift_Id]
      ,[StartDate]
      ,[EndDate]

and code behind is
C#
var sectorEmployees = _service.GetEmployeeStatusBySector_Id(sectorId).OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).ToList();
                var employeesbeat = sectorEmployees.Where(x => x.DutyType_Id == 4 && x.EmployeeStatusType_Id == 1).OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).ToList();
var absentDetail = sectorEmployees.Where(x => x.EmployeeStatusType_Id == 2).OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).ToList(); 
            foreach (var absent in absentDetail)
            {
                Employee emp = _service.GetEmployee(absent.Employee_Id);
                dataset.AbsentDetail.Rows.Add(emp.Name, emp.Id, absent.StartDate);
            }

            var shortleave = sectorEmployees.Where(x => x.EmployeeStatusType_Id == 3).OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).ToList(); 
            foreach (var shrtleave in shortleave)
            {
                 Employee emp = _service.GetEmployee(shrtleave.Employee_Id);
                dataset.ShortLeaveDetail.Rows.Add(emp.Name, emp.Id, shrtleave.StartDate);
            } 

The problem is that one employee is repeated in more than one status, but my requirement is as one employee last status is falling only in one status then its must goes only in one??hope u got??

My table structure and data:
emp_Id status_Id startdate          EndDate
227             1       2015-1-23      2015-2-02
227             2       2015-2-2      2015-2-03
227             3       2015-2-2      2015-2-04

I need to check the last status and put employee into that system.
Posted
Updated 5-Feb-15 20:16pm
v4
Comments
_Asif_ 6-Feb-15 0:45am    
What is your table structure and your desire output. Please improve your question
Sinisa Hajnal 6-Feb-15 2:20am    
How about you take the most generic of the selectors, that is, take all sector employees and filter out by .OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).ToList();

Then the rest of the filters to the resulting list instead of repeating it on each...it will get much readable among other things.

Alternative to this...instead of x.EmployeeStatusTypeId == 2 use Max(x.EmployeeStatusId) == 2

Finally, consider putting statusId numbers into ENUM type so the code gets easier to read instead of relying on knowing the magic numbers meaning.
Herman<T>.Instance 6-Feb-15 4:02am    
MS SQL, Linq?
Sajid227 6-Feb-15 4:46am    
yes

C#
 //Assuming that you have a collection of type Employees named employees

//Order the collection by the EndDate
 var orderedQuery = employees.OrderByDescending(e => e.EndDate).
//Use 'Distinct' to remove duplicate emp_id entries from the collection
Distinct(new DistinctEmployeeComparer()).
//finally group by status_Id
GroupBy(e=>e.status_id);
//You can Enumerate the groups like this
            foreach (IGrouping<int,Employee> grouping in orderedQuery)
            {
                Console.WriteLine("Key: " + grouping.Key);

                foreach (Employee employee in grouping)
                    Console.WriteLine(employee.emp_id + "  " + employee.EndDate);
                Console.WriteLine("***********************");
            }
//You need to have a Comparer class defined something like this

  public class DistinctEmployeeComparer : IEqualityComparer<Employee> {

    public bool Equals(Employee x, Employee y) {
        return x.emp_id == y.emp_id ;
    }

      public int GetHashCode(Employee obj)
      {
          return obj.emp_id;
      }
  }
 
Share this answer
 
v4
i am facing that problem that in my table multiple employee record are being store ,and to get last record of each employee we have to divide it that employee in to different duties and status that are listed on the report.the following code make it easy not to get only last record from multiple record of same employee but also divide it accordingly:
C#
var shortleave = sectorEmployees.OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).Where(x => x.EmployeeStatusType_Id == 3).ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900