从每个部门列表中获取50个员工数据

时间:2015-08-31 06:23:54

标签: c# asp.net-mvc linq

这是我的代码

public class ViewModel 
{
    public List<Department> Departments{ get; set; }
}
public class Department
{
    public Department();

    public List<Employee> Employees{ get; set; }
}
public void post(ViewModel response)
{
    ViewModel   viewModel= new ViewModel
    {               
      Departments= new List<Department>(){ 
        new Department{ Employees= response.Departments[0].Employees.Take(50).ToList() } }
    };          
}

在上面的代码中获得了来自第一部门的50名员工数据,但我希望得到每个部门的员工数据

2 个答案:

答案 0 :(得分:1)

试试这个:

ViewModel viewModel= new ViewModel
{               
    Departments = response.Departments.Select(x => 
    { 
        x.Employees = x.Employees.Take(50).ToList(); 
        return x;
    })
    .ToList()
};

见工作filddle

答案 1 :(得分:0)

我想你只需要Projection,就像这样: -

ViewModel viewModel= new ViewModel { 
       Departments = List<response.Select(x => 
                   new Department { Employees = x.Employees.Take(50) }).ToList();
      };