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.

2

5 Answers 5

6

Have try making it

.Distinct().ToList();

You can refer here LINQ: Distinct values

Sign up to request clarification or add additional context in comments.

1 Comment

it is not working for me . i want do same in C# object list .
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();

2 Comments

After getting that list from objEmployeeTransferList. And then only i have to filter by distict(empid). how i have to do that.
@SaiAyyappsSekaran first sample gets unique ids from objEmployeeTransferList
3

Get Distinct using GroupBy

objEmployeeTransferList.GroupBy(x => x.empId).Select(g => g.First()).ToList();

Comments

0

Have you try:

objEmployeeTransferList = TimecardDC.MAS_EMPLOYEE_TRANSFER.Where(
   employee => employee.HR_ADMIN_IND=="Y").Distinct().ToList();     

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.