有一个组织有几个部门,每个部门都有几个员工。
我创建了以下对象模型:
public class Organisation
{
public int Code { get; set; }
public string Type { get; set; }
public string Name { get; set; }
public List<Department> Departments { get; set; }
}
public class Department
{
public int Code { get; set; }
public string Name { get; set; }
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int Code { get; set; }
public string Name { get; set; }
}
现在,我有这些组织的列表并使用 LINQ,我想对输出进行排序/排序,如下所示:
1) 组织:按代码和名称排序
2)部门:按代码和名称排序
3) 员工:按代码和姓名排序
以下是我填充的一些测试数据:
var britishTelecomLtd = new Organisation
{
Code = 8,
Name = "British Telecom Ltd",
Type = "Institutional",
Departments = new List<Department>
{
new Department
{
Code = 6,
Name = "Finance",
Employees = new List<Employee>
{
new Employee
{
Code = 5,
Name = "Peter"
},
new Employee
{
Code = 2,
Name = "James"
},
new Employee
{
Code = 6,
Name = "Andrew"
}
}
},
new Department
{
Code = 5,
Name = "Accounts",
Employees = new List<Employee>
{
new Employee
{
Code = 15,
Name = "Jane"
},
new Employee
{
Code = 22,
Name = "John"
},
new Employee
{
Code = 16,
Name = "Mark"
}
}
}
}
};
var virginMediaLtd = new Organisation
{
Code = 5,
Name = "Virgin Media Ltd",
Type = "Institutional",
Departments = new List<Department>
{
new Department
{
Code = 6,
Name = "Sales",
Employees = new List<Employee>
{
new Employee
{
Code = 5,
Name = "Peter"
},
new Employee
{
Code = 2,
Name = "James"
},
new Employee
{
Code = 6,
Name = "Andrew"
}
}
},
new Department
{
Code = 5,
Name = "Support",
Employees = new List<Employee>
{
new Employee
{
Code = 15,
Name = "Jane"
},
new Employee
{
Code = 22,
Name = "John"
},
new Employee
{
Code = 16,
Name = "Mark"
}
}
}
}
};
var pcWorldLtd = new Organisation
{
Code = 18,
Name = "PC World Ltd",
Type = "Retail",
Departments = new List<Department>
{
new Department
{
Code = 6,
Name = "Marketing",
Employees = new List<Employee>
{
new Employee
{
Code = 15,
Name = "Jane"
},
new Employee
{
Code = 22,
Name = "John"
},
new Employee
{
Code = 16,
Name = "Mark"
}
}
},
new Department
{
Code = 5,
Name = "Customer Services",
Employees = new List<Employee>
{
new Employee
{
Code = 5,
Name = "Kelly"
},
new Employee
{
Code = 2,
Name = "Jenny"
},
new Employee
{
Code = 6,
Name = "Tricia"
}
}
}
}
};
var blueCatLtd = new Organisation
{
Code = 3,
Name = "Blue Cat Music Ltd",
Type = "Retail",
Departments = new List<Department>
{
new Department
{
Code = 6,
Name = "Sales",
Employees = new List<Employee>
{
new Employee
{
Code = 5,
Name = "Peter"
},
new Employee
{
Code = 2,
Name = "James"
},
new Employee
{
Code = 6,
Name = "Andrew"
}
}
},
new Department
{
Code = 5,
Name = "Warehouse",
Employees = new List<Employee>
{
new Employee
{
Code = 5,
Name = "Andy"
},
new Employee
{
Code = 2,
Name = "Robert"
},
new Employee
{
Code = 6,
Name = "Dave"
}
}
}
}
};
var organisations = new List<Organisation>
{
britishTelecomLtd,
virginMediaLtd,
pcWorldLtd,
blueCatLtd
};
在这里,我将数据添加到字典中:
var legalEntitiesCollectionByType = new Dictionary<string, ICollection<Organisation>>
{
{
"Institutional", organisations
.Where(x => x.Type == "Institutional")
.OrderBy(x => x.Code).ThenBy(x => x.Name)
.ToList()
},
{
"Retail", organisations
.Where(x => x.Type == "Retail")
.OrderBy(x => x.Code).ThenBy(x => x.Name)
.ToList()
}
};
这样做,排序只发生在组织级别,而不是部门或员工级别。
我的问题是,如何在填充上面的字典时在对象层次结构中的所有 3 个级别上实现排序?
干杯