使用LINQ的分层数据表示

本文关键字:数据表示 分层 LINQ 使用 | 更新日期: 2023-09-27 18:02:50

总之,我正试图用LINQ对下面的类进行分层数据表示。有谁能帮我一下吗

public class Employee
    {
       public Employee(int empId,int? managerId)
       {
           this.Id = empId;
           this.MangerId = managerId;
           this.Children = new List<Employee>();
       }
        public int Id { get; set; }
        public int? MangerId { get; set; }
        public string EmployeeName { get; set; }
        public List<Employee> Children { get; set; }
    }

示例数据
var empList = new List<Employee>
             {
                 new Employee(1,2){EmployeeName = "Joseph"},
                 new Employee(2,3){EmployeeName = "Smith"},                 
                 new Employee(3,4){EmployeeName = "Bob"},
                 new Employee(4,null){EmployeeName = "Doug"},
                 new Employee(5,2){EmployeeName = "Dave"},
                 new Employee(6,4){EmployeeName = "Allan"}
             };

,输出应该像这样

/*道格鲍勃艾伦史密斯约瑟夫戴夫

*/

如有任何帮助,不胜感激

edit:高层员工的managerId将为null

使用LINQ的分层数据表示

使用您的模型,Employee模型不会直接链接到它们自己。相反,我们必须使用它们的标识符遍历层次结构。

首先,我们得到根雇员:
var rootEmp = EmpList.Single(e => e.MangerId == null);
然后,使用递归函数遍历层次结构:
string WalkEmployees(Employee root)
{
    // Create the container of the names
    var builder = new StringBuilder();
    // Get the children of this employee
    var children = EmpList.Where(e => e.MangerId == root.Id);
    // Add the name of the current employee in the container
    builder.Append(root.EmployeeName + " ");
    // For each children, walk them recursively
    foreach (var employee in children)
    {
        builder.Append(WalkEmployees(employee));
    }
    // Return the container of names
    return builder.ToString();
}
最后,调用函数:
WalkEmployees(rootEmp);

本质上,递归函数垂直遍历层次结构:

Doug
——鲍勃
——史密斯
——约瑟夫
——Dave
——艾伦

尽管如此,为了让Allan紧跟在Bob之后,您希望进行水平行走。为此,我为您的员工添加了一个视图模型,该模型描述了他们在层次结构中的级别。

public class EmployeeViewModel
{
    public EmployeeViewModel(Employee employee, int level)
    {
        Employee = employee;
        Level = level;
    }
    public Employee Employee { get; set; }
    public int Level { get; set; }
}

员工步行功能变为:

IEnumerable<EmployeeViewModel> WalkEmployees(Employee root, int level)
{
    // Create the container of the employees
    var container = new List<EmployeeViewModel> {new EmployeeViewModel(root, level)};
    // Get the children of this employee
    var children = EmpList.Where(e => e.MangerId == root.Id);
    // For each children, walk them recursively
    foreach (var employee in children)
    {
        container.AddRange(WalkEmployees(employee, level + 1));
    }
    // Return the container
    return container;
}

及其调用:

var rootEmp = EmpList.Single(e => e.MangerId == null);
var employees = WalkEmployees(rootEmp, 0);
// Order the employees by its level in the hierarchy
var orderedEmployees = employees.OrderBy(vm => vm.Level);
// Display the names
foreach (var orderedEmployee in orderedEmployees)
{
    Console.Write(orderedEmployee.Employee.EmployeeName + " ");
}

你会得到这样的结果:

Doug
——鲍勃
——艾伦
——史密斯
——约瑟夫
——Dave

你们的模型很难处理,因为模型之间缺乏联系。这里有一个更有力的建议:
public class Employee
{
    #region Constructors
    public Employee()
    {
        Employees = new List<Employee>();
    }
    public Employee(string name) : this()
    {
        Name = name;
    }
    public Employee(string name, Employee manager) : this(name)
    {
        Manager = manager;
    }
    public Employee(string name, Employee manager, params Employee[] employees) : this(name, manager)
    {
        Employees.AddRange(employees);
    }
    #endregion
    #region Properties
    public List<Employee> Employees { get; set; }
    public int Id { get; set; }
    public Employee Manager { get; set; }
    public string Name { get; set; }
    #endregion
}

您现在可以像这样生成您的员工:

/// <summary>
/// Generates the employees in a hierarchy way.
/// </summary>
/// <returns>Returns the root employee.</returns>
Employee GenerateEmployees()
{
    var doug = new Employee("Doug");
    doug.Employees.Add(new Employee("Allan", doug));
    var bob = new Employee("Bob", doug);
    doug.Employees.Add(bob);
    var smith = new Employee("Smith", bob);
    bob.Employees.Add(smith);
    smith.Employees.Add(new Employee("Joseph", smith));
    smith.Employees.Add(new Employee("Dave", smith));
    return doug;
}

walk函数变为:

string WalkEmployees(Employee root)
{
    var builder = new StringBuilder();
    builder.Append(root.Name + " ");
    foreach (var employee in root.Employees)
    {
        builder.Append(WalkEmployees(employee));
    }
    return builder.ToString();
}

如果你使用EntityFramework来设计一个使用导航属性的数据库,这个实现会更有意义。

尝试:

string text = string.Join(" ",
    from i in empList
    orderby i.MangerId.HasValue, i.MangerId descending
    select i.EmployeeName);