以下是我的示例数据。两家公司有两名员工。
List<Company> companies = new List<Company>
{
new Company
{
Name = "ABC Company",
Address = "No.1 St. USA",
Employees = new List<Employee> { new Employee { Name = "John", Age = 30 }, new Employee { Name = "David", Age = 20 } }
},
new Company
{
Name = "DEF Company",
Address = "No.2 St. USA",
Employees = new List<Employee> { new Employee { Name = "Michael", Age = 22 }, new Employee { Name = "Jason", Age = 27 } }
}
};
我尝试使用以下语法检索所有员工:
var employees = companies.Select(c => c.Employees).ToList();
然而,它给了我
当前上下文中不存在“员工”这个名称
我在运行时观看它。我的预期结果是拥有全部4名员工的List<Employee>
。任何建议将不胜感激。
答案 0 :(得分:5)
您应该使用SelectMany
代替Select
。
您显示的错误与该查询无关。在尝试观察此变量时,您很可能不在正确的范围内。