如何获取所有员工的最新记录列表 [英] how to get list of latest record of all employee

查看:117
本文介绍了如何获取所有员工的最新记录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,其中我存储了同一个员工的多个记录,并且还有多个员工,我想获得所有员工的最后记录,然后根据他们的状态划分它就像在工作上empstatus_Id 1,缺席empstatus_Id 2,离开empstatus_Id 3,我想得到所有员工最新记录的清单。

<前lang =SQL> [Id]
,[Employee_Id]
,[ Rank_Id]
,[EmployeeStatusType_Id]
,[Sector_Id]
,[DutyType_Id]
,[EmployeeShift_Id]
,[StartDate]
,[EndDate]



和背后的代码是

  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)。选择(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 缺席 absentDetail)
{
员工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)。选择(x = > x.First()) .ToList();
foreach var shrtleave in shortleave)
{
员工emp = _service.GetEmployee(shrtleave.Employee_Id);
dataset.ShortLeaveDetail.Rows.Add(emp.Name,emp.Id,shrtleave.StartDate);
}



问题是一个员工在多个状态下重复,但我的要求是一个员工最后一个状态只在一个状态下降然后它必须只有一个??希望你得到??



我的表结构和数据:

 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-2- 04



我需要检查最后状态并将员工放入该系统。

解决方案

  //  假设您有一个名为employees的Employees类型的集合 

// 按EndDate订购集合
var orderedQuery = employees.OrderByDescendin g(e = > e.EndDate)。
// 使用Distinct从集合中删除重复的emp_id条目
区别( new DistinctEmployeeComparer())。
// 最后按status_Id分组
GroupBy(e => e。 STATUS_ID);
// 您可以枚举这样的组
foreach (IGrouping< int,Employee> grouping in orderedQuery)
{
Console.WriteLine( 密钥: + grouping.Key);

foreach (员工员工 分组)
Console.WriteLine (employee.emp_id + + employee.EndDate);
Console.WriteLine( ***************** ******);
}
// 你需要定义一个像这样的Comparer类

public class DistinctEmployeeComparer:IEqualityComparer< Employee> {

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

public int GetHashCode(Employee obj)
{
return obj.emp_id;
}
}


我面临的问题是,在我的表中存储多个员工记录,并获取最后一条记录每个员工我们必须将该员工分成报告中列出的不同职责和状态。以下代码使得不容易从同一员工的多个记录中获得最后记录,但也相应地划分:

  var  shortleave = sectorEmployees.OrderByDescending(x = >  x.EndDate).GroupBy(x = >  x.Employee_Id)。选择(x = >  x.First())。其中(x = >  x.EmployeeStatusType_Id ==  3 )。 ToList(); 


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.

[Id]
      ,[Employee_Id]
      ,[Rank_Id]
      ,[EmployeeStatusType_Id]
      ,[Sector_Id]
      ,[DutyType_Id]
      ,[EmployeeShift_Id]
      ,[StartDate]
      ,[EndDate]


and code behind is

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.

解决方案

 //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;
      }
  }


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:

var shortleave = sectorEmployees.OrderByDescending(x => x.EndDate).GroupBy(x => x.Employee_Id).Select(x => x.First()).Where(x => x.EmployeeStatusType_Id == 3).ToList();


这篇关于如何获取所有员工的最新记录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆