1

这是我的数据:

private List<Department> Data
{
    get
    {
        return new List<Department>
        {
            new Department{
                Id = 1, 
                Name = "Tech",
                Employees = new List<Employee>{
                    new Employee{Name = "x", Id = 1 },
                    new Employee{ Name = "y", Id = 2}
                }
            },
            new Department{
                Id = 2,
                Name = "Sales",
                Employees = new List<Employee>{
                    new Employee{Name = "a", Id = 3},
                    new Employee {Name = "b", Id = 4}
                }
            }
        };
    }
}

在这里,我得到了所有员工及其相应部门的列表:

List<Employee> employees = (from department in Departments
                       let d = department
                       from e in d.Employees
                       select new Employee{
                            Id = e.Id,
                            Name = e.Name
                            Department = d
                       }).ToList();

困扰我的是我必须重新创建我的 Employee 对象才能将适当的部门附加到它。有没有一种方法可以编写我的 LINQ 语句而不必重新创建 Employee?

可能有更好的方式来表达这个问题——所以请随时告诉我是否有。

编辑 我走这条路的原因是我通过序列化我的部门来存储我的数据:

[
    {
        "Id":1,
        "Name":"Sales",
        "Employees":[{"Id":2,"Name":"x"},{"Id":1,"Name":"y"}]
    },
    {
        "Id":2,
        "Name":"Tech",
        "Employees":[{"Id":3,"Name":"d"},{"Id":4,"Name":"f"}]
    }

]
4

4 回答 4

1

It looks like you want to use LINQ to update an instance. This is not the intended use. Use LINQ to query the instances you want to have, and then loop over the results to update. (non-nested) Loops are not evil.

var query = 
  from d in Departments
  from e in d.Employees
  select new { Employee = e, Department = d };

foreach(var x in query)
{
  x.Employee.Department = x.Department;
}
于 2013-07-18T12:52:00.557 回答
1

Using let is redundant and not useful in your example query.

Besides, LINQ is not the right tool here. You want to affect the state of the objects you're querying (i.e. creating side-effects), which is generally not recommended.

By direct comparison, this is a better alternative to what you're trying do to:

 foreach(var department in Departments)
 foreach(var employee in department.Employees)
     employee.Department = department;

If you can however, you should do the department assignment at the time you add the employees to the department, either in an AddEmployee method in the Department class, or maybe in a Employee.Department property setter.

于 2013-07-18T12:52:35.860 回答
1

如果您真的非常想要,您可以使用-let子句来产生副作用,因为赋值表达式会返回一个值:

List<Employee> employees = (from department in Departments
                            from e in department.Employees
                            let _ = e.Department = department
                            select e).ToList();

我也完全同意BrokenGlass ...

于 2013-07-18T12:45:55.213 回答
1

首先你不应该有这个问题 - 你应该Employee在最初创建实例时完全构建实例,而不是稍后 - 如果员工需要使用一个部门,你应该添加一个允许/强制提供它的构造函数:

public Employee(int id, string name, Department department)
{
   ...
}
于 2013-07-18T12:43:04.200 回答