gpt4 book ai didi

c# - LINQ 获取列表列表

转载 作者:行者123 更新时间:2023-11-30 19:42:02 25 4
gpt4 key购买 nike

  public class Employee
{
public string Name{get;set;}
public List<Department> Department { get; set; }
public string Company{get;set;}
}

public class Department
{
public string Name { get; set; }
public string Location { get; set; }
}

List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { Company = "Dell", Name = "ABC" });
employees.Add(new Employee() { Company = "Dell", Name = "Aakash" });
employees.Add(new Employee() { Company = "CSC", Name = "Vaibhav" });
employees.Add(new Employee() { Company = "TCS", Name = "Sambhav" });

employees[0].Department = new List<Department>();
employees[0].Department.Add(new Department() { Location = "Delhi", Name = "HR" });
employees[0].Department.Add(new Department() { Location = "Delhi", Name = "Recruitment" });

employees[1].Department = new List<Department>();
employees[1].Department.Add(new Department() { Location = "Noida", Name = "EAO" });
employees[1].Department.Add(new Department() { Location = "Delhi", Name = "Arch" });

employees[2].Department = new List<Department>();
employees[2].Department.Add(new Department() { Location = "Denmark", Name = "Scandi" });
employees[2].Department.Add(new Department() { Location = "Noida", Name = "SAG" });

employees[3].Department = new List<Department>();
employees[3].Department.Add(new Department() { Location = "Mumbai", Name = "NSE" });

我需要编写一个 lambda 表达式来选择 Department Location 为 Noida 的所有员工

最佳答案

使用Any扩展方法:

var results = employees.Where(e => e.Department.Any(d => d.Location == "Noida"))
.ToList();

关于c# - LINQ 获取列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18182800/

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