linq to sql recursive query

后端 未结 4 473
感情败类
感情败类 2020-12-06 05:12
EmployeeId  Name  ManagerId
------------------------------
1           A     null
2           B     null
3           C     1
4           D     3
5           E     2
         


        
4条回答
  •  难免孤独
    2020-12-06 05:51

    You could do something like

        int id = 5;
        do
        {
            employee= employeedata.FirstOrDefault(e => e.EmployeeId == id);
    
        } while (employee != null && (id = employee.ManagerId) != 0);
    

    but it's a rather dangerous thing to do since it can get stuck in an infinite loop. As far as I know there's no way to make a recursive query directly unless you write a stored procedure.

提交回复
热议问题