Creating a custom class for LINQ queries

I have a collection base class that simply wraps a list of a user object stored in a List variable so that I can use this [] indexing.

However, now I want to make the class available so that it can be run from an element in the LINQ query collection

Using a simplified analogy, I can get an employee from the list just by doing

Employeex = MyStaffList[Payroll];

... but now I want to do

var HREmps = from emp in StaffList
             where emp.Department == "HR"
             select emp;

The following is the definition of the proto type ....

public class StaffList
{
    List<Employee> lst = new List<Employee>();

    public StaffList()
    {
        /* Add Employees to the list */
    }

    public Employee this[string payroll]
    {
        get
        {
            Employee oRet = null;
            foreach (Employee emp in lst)
            {
                if (emp.Payroll.Equals(payroll, StringComparison.InvariantCultureIgnoreCase))
                {
                    oRet = emp ;
                    break;
                }
            }
            return (oRet);
        }
    }
}

public class Employee
{
    public string Payroll;
    public string Department;
    .
    .
    .
    .
}
+4
source share
1 answer

LINQ, IEnumerable<Employee>. ( , return lst.GetEnumerator();.

, , List<Employee> , object. , ?

+11

All Articles