[英]Creating a new object in LINQ query
I have a list of departments. 我有一个部门清单。 And each department has a list of employees in it. 每个部门都有一份员工清单。 I need to return all employees from this list who were hired after January 15, 2015. But I need to return a list of "Roster" ie List<Roster>
. 我需要退回2015年1月15日之后被雇用的所有员工。但我需要返回“名册”列表,即List<Roster>
。 I need help with the LINQ query. 我需要LINQ查询的帮助。
Here's my Department class 这是我的系类
public class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public List<Employee> Employees { get; set; } = new List<Employee>();
public string Location { get; set; }
}
And my employee class looks like this: 我的员工班看起来像这样:
public class Employee
{
public int EmployeeId { get; set; }
public Guid PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime HireDate { get; set; }
}
Here's my roster class 这是我的名册班
public class Roster
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public DateTime HireDate { get; set; }
}
Here's what my query looks like so far: 这是我的查询到目前为止的样子:
var employees = departments.Where(x => x.Employees).Any(o => o.HireDate >= Convert.ToDateTime("1/15/2015")).Select(i => new Roster { ??? }).ToList();
Please keep in mind, I want to return List<Roster>
and need to include both department and employee information in the result. 请记住,我想返回List<Roster>
并且需要在结果中包含部门和员工信息。 I'm not sure how to get some data from Department object and some from Employee to create the Roster class. 我不知道如何从Department对象获取一些数据,而从Employee获取一些数据来创建Roster类。 Thanks. 谢谢。
var rosters = departments.SelectMany(
x => x.Employees
.Where(y => y.HireDate >= Convert.ToDateTime("1/15/2015"))
.Select(y => new Roster{ DepartmentId = x.DepartmentId, DepartmentName = x.DepartmentName, FirstName = y.FirstName, LastName = y.LastName, Gender = y.Gender, HireDate = y.HireDate})
).ToList();
By popular demand – explanation: 受欢迎的需求 - 解释:
departments.SelectMany(x => x.Employees…)
– joins employees from all departments in single employee list. departments.SelectMany(x => x.Employees…)
- 加入单个员工列表中所有部门的员工。
.Where(…)
– filters only employees that were hired after specific date .Where(…)
- 仅筛选在特定日期之后雇用的员工
.Select(y => new Roster{…})
– creates Roster
object for each selected employee. .Select(y => new Roster{…})
- 为每个选定的员工创建Roster
对象。 At this point we have access to both lambda variables ( x
– represents department, y
– employee) and can use them to fill Roster
object fields. 此时,我们可以访问两个lambda变量( x
- 代表department, y
- employee),并可以使用它们来填充Roster
对象字段。
Warning: Will give duplicates if employee is assigned to more than one department. 警告:如果将员工分配到多个部门,则会给出重复项。
general idea like this one, but it should be refactored 像这样的一般想法,但它应该被重构
var departments = new List<Department>();
var employees = new List<Employee>();
var result = employees
.Where(e => e.HireDate >= Convert.ToDateTime("1/15/2015"))
.Select(e => new Roster
{
FirstName = e.FirstName,
LastName = e.LastName,
Gender = e.Gender,
HireDate = e.HireDate,
DepartmentId = departments.First(d => d.Employees.Any(ee => e.EmployeeId == e.EmployeeId)).DepartmentId,
DepartmentName = departments.First(d => d.Employees.Any(ee => e.EmployeeId == e.EmployeeId)).DepartmentName
});
var rosters = employees.Where(e => e.HireDate > someDate)
.Select(s => new Roster {
LastName = s.FirstName,
Dept = s.Department.DepartmentName
Note - For this to work, You'd need to add department property into the employee object.. 注意 - 为此,您需要将department属性添加到employee对象中。
Or make a join between employees and depts 或者在员工和部门之间进行联接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.