0

I have

IEnumerable<Employee> GetAllEmployees()
{
  return Get<IEmployeeRepository>().GetAll();
}

Where IEmployeeRepository is

public interface IEmployeeRepository
    {
        IEnumerable<Employee> GetAll();  
    }

Employee.cs is as under

public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }        
        public int Age { get; set; }
        public int Salary { get; set; }
    }

Now I want to perform a search on the records obtained from GetAllEmployees() based on the EmployeeId passed. Like as under

public void SearchEmployee(int empId)
        {
            var empRecords = this.GetAllEmployees();
            var filteredRecords = empRecords.
        }

enter image description here

I actually looked into this answer but could not fit into the requirement. What I need to do?

Community
  • 1
  • 1
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173
  • http://stackoverflow.com/questions/7757365/does-linq-work-with-ienumerable – Sajeetharan Apr 06 '15 at 06:43
  • 2
    Might be you are missing namespace declaration on top `using System.Linq`. Also keep in mind `IEnumerable` does not support differed execution like `IQuerable` – Jenish Rabadiya Apr 06 '15 at 06:43
  • 1
    What happens if you change `var empRecords = this.GetAllEmployees();` to `IEnumerable empRecords = this.GetAllEmployees();` ? – SimpleVar Apr 06 '15 at 06:44
  • @Jenish Rabadiya, Excellent. I just saw that in my namespace. – priyanka.sarkar Apr 06 '15 at 06:45
  • @JenishRabadiya What do you mean by *IEnumerable does not support differed execution like IQuerable*. `IEnumerable` do supports deferred execution. In fact `Enumerable.Where`, `Select` etc are lazy. – Sriram Sakthivel Apr 06 '15 at 06:53
  • @SriramSakthivel Yes, you are right. I might used wrong word there. I was meant to say it does not support linq to sql. – Jenish Rabadiya Apr 06 '15 at 06:55

2 Answers2

1

I just figured out that the namespace System.Linq is missing. Else we could even do

foreach (var item in empRecords)
{
   //
}

I will give the credit to @ Jenish Rabadiya, though others have also given correct opinion.

priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173
0

This is the object first you need to convert to list of object and then try to cast it.

 (empRecords as IEnumerable<object>).Cast(....)
Aravind Sivam
  • 1,089
  • 8
  • 23