Get distinct values from list

前端 未结 5 1044
我在风中等你
我在风中等你 2020-12-21 07:46
    public List GetEmployeeTransferListForHR(TimecardDataContext TimecardDC)
    {
        List objEmployee         


        
相关标签:
5条回答
  • 2020-12-21 07:58

    Get Distinct using GroupBy

    objEmployeeTransferList.GroupBy(x => x.empId).Select(g => g.First()).ToList();
    
    0 讨论(0)
  • 2020-12-21 08:11

    There is a distinct method in linq which should do the trick.

    http://msdn.microsoft.com/en-gb/library/bb348436.aspx

    0 讨论(0)
  • 2020-12-21 08:19

    Have you try:

    objEmployeeTransferList = TimecardDC.MAS_EMPLOYEE_TRANSFER.Where(
       employee => employee.HR_ADMIN_IND=="Y").Distinct().ToList();     
    
    0 讨论(0)
  • 2020-12-21 08:22

    Have try making it

    .Distinct().ToList();
    

    You can refer here LINQ: Distinct values

    0 讨论(0)
  • 2020-12-21 08:24
    List<int> ids = objEmployeeTransferList
                       .Select(e => e.empId)
                       .Distinct()
                       .ToList();
    

    Also you can make this on server side without creating in-memory employee list with all admin records:

    List<int> ids = TimecardDC.MAS_EMPLOYEE_TRANSFER
                       .Where(e => e.HR_ADMIN_IND == "Y")
                       .Select(e => e.empId)
                       .Distinct()
                       .ToList();
    
    0 讨论(0)
提交回复
热议问题