gpt4 book ai didi

c# - 使用 lambda 从主从列表中选择详细信息

转载 作者:太空宇宙 更新时间:2023-11-03 20:13:09 24 4
gpt4 key购买 nike

下面是我的示例数据。两家公司有两名员工。

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();

但是,它给了我

The name 'employees' does not exist in the current context

当我在运行时观看它时。我的预期结果是 List<Employee>拥有所有4名员工。任何建议将不胜感激。

最佳答案

您应该使用 SelectMany 而不是 Select

您显示的错误与该查询无关。很可能您在尝试查看此变量时不在正确的范围内。

关于c# - 使用 lambda 从主从列表中选择详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18613205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com