C# 如何将两个类之间的1对多关系转换为

C# 如何将两个类之间的1对多关系转换为,c#,.net,list,C#,.net,List,我有两个班,有一对多的关系。需要将部门员工的对象转换为员工部门 //Employee to Department (1-n) relation public class DepartmentEmployeesEntity { public DepartmentEntity Department { get; set; } public List<EmployeeEntity> Employees { get; set; } } //Employee to Dep

我有两个班,有一对多的关系。需要将
部门员工
的对象转换为
员工部门

//Employee to Department (1-n) relation
public class DepartmentEmployeesEntity
{
    public DepartmentEntity Department { get; set; }

    public List<EmployeeEntity> Employees { get; set; }

}

//Employee to Department (1-n) relation
public class EmployeeDepartmentsEntity
{
    public EmployeeEntity Employee { get; set; }

    public List<DepartmentEntity> Departments { get; set; }   
}

public class University
{
    public List<DepartmentEmployeesEntity> DepartmentEmployees { get; set; }

    public List<EmployeeDepartmentsEntity> EmployeeDepartments { get; set; }   
}
//员工与部门(1-n)的关系
公共类部门员工意识
{
公共部门实体部门{get;set;}
公共列表雇员{get;set;}
}
//员工与部门(1-n)的关系
公共类EmployeeDepartmentsEntity
{
公共雇员实体雇员{get;set;}
公共列表部门{get;set;}
}
公立大学
{
公共列表部门员工{get;set;}
公共列表EmployeeDepartments{get;set;}
}

部门员工的数据以json格式存储在数据库中。如何将
DepartmentEmployees
对象转换为
EmployeeDepartments
对象

在转换数据时,使用字典加快查找员工的速度

var entitiesByEmployee = new Dictionary<Employee, EmployeeDepartmentsEntity>();
var entitiesByEmployee=new Dictionary();
然后在数据上循环,在字典中创建或更新EmployeeDepartmentsEntity条目

var allDepartmentEmployeesEntities = dbContext.DepartmentEmployeesEntity.ToArray();
foreach (var departmentEmployee in allDepartmentEmployeesEntities) {
   foreach(var employee in departmentEmployee.Employees) {
      if (entitiesByEmployee.ContainsKey(employee)) {
          // this employee already showed up in another department, update his entry
          entitiesByEmployee[employee].Departments.Add(departmentEmployee.Department)
      }
      else {
          // this employee has not been processed yet, create new entry
          var newEntry = new EmployeeDepartmentsEntity {
              Employee = employee,
              Departments = new List<DepartmentEntity> {departmentEmployee.Department}
          };
          entitiesByEmployee.Add(employee, newEntry);
      }
   }
}
var allDepartmentEmployeesEntities=dbContext.DepartmentEmployeesEntity.ToArray();
foreach(所有部门员工属性中的var部门员工){
foreach(部门中的var员工employee.Employees){
if(实体按雇员。容器(雇员)){
//该员工已出现在其他部门,请更新其条目
entitiesByEmployee[employee].Departments.Add(departmentEmployee.Department)
}
否则{
//此员工尚未处理,请创建新条目
var newEntry=新员工部门意识{
雇员=雇员,
部门=新列表{departmentEmployee.Department}
};
entitiesByEmployee.Add(员工,新条目);
}
}
}
最后,您的结果是字典中的所有值:

List<EmployeeDepartmentsEntity> employeeDepartments = entitiesByEmployee.Values.ToList();
List employeeDepartments=entitiesByEmployee.Values.ToList();

任何解决方案都需要将初始列表(dep->employees)展平,然后按员工分组(emp->departments)

使用linq可以执行以下两个步骤:

List<DepartmentEmployeesEntity> dep = new List<DepartmentEmployeesEntity>();
dep = PopulateData();
var step1 = dep.SelectMany(d => d.Employees, (d, e) => new { d.Department, employee = e});
List<EmployeeDepartmentsEntity> empDepList = step1.GroupBy(e => e.employee).Select(g => new EmployeeDepartmentsEntity () { Employee = g.Key, Departments = g.Select(x => x.Department).ToList() }).ToList();

只需做后处理。将两者序列化是没有效率的,只能作为后期处理。可悲的是,“有效的方式”受制于意见,也脱离了人们的话题SO@MickyD我需要答案,我从乔治那里得到了答案。我需要知道如何先处理它,然后如果有多个答案,我需要有效的一个。那个这是我以有效的方式添加的唯一原因。如果你仍然觉得问题必须解决,别担心。
public class DepartmentEmployeesEntity
{
    public string Department { get; set; }
    public List<string> Employees { get; set; }
}