简体   繁体   English

如何在IEnumerable上执行LINQ <Employee> / IEnumerable <object> ?

[英]How to perform LINQ on IEnumerable<Employee> / IEnumerable<object>?

I have 我有

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

Where IEmployeeRepository is IEmployeeRepository在哪里

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

Employee.cs is as under Employee.cs如下

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. 现在,我想基于传递的EmployeeId对从GetAllEmployees()获得的记录进行搜索。 Like as under 像下

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

在此处输入图片说明

I actually looked into this answer but could not fit into the requirement. 我实际上调查了这个答案,但不符合要求。 What I need to do? 我需要做什么?

I just figured out that the namespace System.Linq is missing. 我只是发现名称空间System.Linq丢失了。 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. 我会感谢@ Jenish Rabadiya,尽管其他人也给出了正确的意见。

首先,这是对象,您需要转换为对象列表,然后尝试投射它。

 (empRecords as IEnumerable<object>).Cast(....)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM