使用C#更新层次结构数据中的字段

时间:2022-09-29 07:17:45

I've a table contains,

我有一张桌子,

EmployeeID, ManagerID & EmployeeOrder

I need to write c# code or sql statment to update the EmployeeOrder for all records according to the managerID relation to be like:

我需要编写c#代码或sql语句来根据managerID关系为所有记录更新EmployeeOrder:

使用C#更新层次结构数据中的字段

1 个解决方案

#1


0  

You can write a function to update the EmployeeOrder of one hierarchy and call it recursively:

您可以编写一个函数来更新一个层次结构的EmployeeOrder并递归调用它:

void Update(string ManagerID){
    var employees = DB.Employees.Where(e=>e.ManagerID == ManagerID).ToArray();
    for(int i=0;i<employees.Length;i++){
        employees[i].EmployeeOrder = m.EmployeeOrder + i.ToString("000");
        Update(Employees[i].EmployeeID);
    }
}

As with any recursive methods, you have to kick start it. In your case perhaps you would use something like:

与任何递归方法一样,您必须启动它。在你的情况下,你可能会使用类似的东西:

Update(null);

That is, of course, assuming that you're using LINQ to SQL or EntityFramwork or similar so you can use LINQ to access your database. Optionally you can write SQL statement equivalent or Stored Procedure equivalent of the above algorithm.

当然,假设您正在使用LINQ to SQL或EntityFramwork或类似工具,那么您可以使用LINQ来访问您的数据库。您可以选择编写与上述算法等效的SQL语句等效或存储过程。

#1


0  

You can write a function to update the EmployeeOrder of one hierarchy and call it recursively:

您可以编写一个函数来更新一个层次结构的EmployeeOrder并递归调用它:

void Update(string ManagerID){
    var employees = DB.Employees.Where(e=>e.ManagerID == ManagerID).ToArray();
    for(int i=0;i<employees.Length;i++){
        employees[i].EmployeeOrder = m.EmployeeOrder + i.ToString("000");
        Update(Employees[i].EmployeeID);
    }
}

As with any recursive methods, you have to kick start it. In your case perhaps you would use something like:

与任何递归方法一样,您必须启动它。在你的情况下,你可能会使用类似的东西:

Update(null);

That is, of course, assuming that you're using LINQ to SQL or EntityFramwork or similar so you can use LINQ to access your database. Optionally you can write SQL statement equivalent or Stored Procedure equivalent of the above algorithm.

当然,假设您正在使用LINQ to SQL或EntityFramwork或类似工具,那么您可以使用LINQ来访问您的数据库。您可以选择编写与上述算法等效的SQL语句等效或存储过程。