Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get distinct values from list

Tags:

c#

asp.net

linq

    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.

like image 302
Ayyappan Sekaran Avatar asked Oct 23 '25 22:10

Ayyappan Sekaran


2 Answers

Have try making it

.Distinct().ToList();

You can refer here LINQ: Distinct values

like image 127
c0dem0nkey Avatar answered Oct 26 '25 11:10

c0dem0nkey


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();
like image 30
Sergey Berezovskiy Avatar answered Oct 26 '25 12:10

Sergey Berezovskiy