4
    public List<MAS_EMPLOYEE_TRANSFER> GetEmployeeTransferListForHR(TimecardDataContext TimecardDC)
    {
        List<MAS_EMPLOYEE_TRANSFER> objEmployeeTransferList = null;
        try
        {
            objEmployeeTransferList = new List<MAS_EMPLOYEE_TRANSFER>();
            objEmployeeTransferList = TimecardDC.MAS_EMPLOYEE_TRANSFER.Where(
                employee =>
                    employee.HR_ADMIN_IND=="Y").ToList();                
        }
        finally
        {
        }
        return objEmployeeTransferList;
    }

It shows all list of values where hr admin indicator=yes. But I have to get hr admin=yes and distinct(empid) from the table MAS_EMPLOYEE_TRANSFER. How to get distinct empId from the the objEmployeeTransferList.

spajce
  • 7,044
  • 5
  • 29
  • 44
Ayyappan Sekaran
  • 986
  • 4
  • 12
  • 27
  • Try doing `groupby`empId and then selecting. have a look [LINQ Select distinct from a List?](http://stackoverflow.com/questions/7572006/linq-select-distinct-from-a-list) will help you – huMpty duMpty Feb 26 '13 at 10:18
  • Check out this question: http://stackoverflow.com/questions/912188/linq-distinct-by-name-for-populate-a-dropdown-list-with-name-and-value – default locale Feb 26 '13 at 10:20

5 Answers5

6

Have try making it

.Distinct().ToList();

You can refer here LINQ: Distinct values

Community
  • 1
  • 1
c0dem0nkey
  • 616
  • 4
  • 13
4
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();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

Get Distinct using GroupBy

objEmployeeTransferList.GroupBy(x => x.empId).Select(g => g.First()).ToList();
Kiran Solkar
  • 1,204
  • 4
  • 16
  • 29
0

Have you try:

objEmployeeTransferList = TimecardDC.MAS_EMPLOYEE_TRANSFER.Where(
   employee => employee.HR_ADMIN_IND=="Y").Distinct().ToList();     
Habibillah
  • 27,347
  • 5
  • 36
  • 56
0

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

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

lparry
  • 86
  • 3