读书人

递归算法求部门以及其上属部门的人数有

发布时间: 2012-10-18 13:46:56 作者: rapoo

递归算法求部门以及其下属部门的人数问题
下面这个写法得到的结果不正确,各位帮忙看看,谢谢

C# code
public static int GetStaffNumByUpLeaderId(int UpleaderId, int parentDeptId)        {            int countStaffNum = 0;            StringBuilder strSql1 = new StringBuilder();            if (UpleaderId > 0)            {                strSql1.Append("select deptId,parentDeptId,deptUpLeaderId FROM Department");                strSql1.Append(" where parentDeptId=" + parentDeptId);                strSql1.Append(" and deptUpLeaderId=" + UpleaderId);            }            else            {                strSql1.Append("select deptId FROM Department");                strSql1.Append(" where parentDeptId=" + parentDeptId);            }            DataTable dt1 = DbHelperSQL.Query(strSql1.ToString()).Tables[0];            if (dt1.Rows.Count>0)            {                for (int i = 0; i < dt1.Rows.Count; i++)                {                    StringBuilder strSql2 = new StringBuilder();                    strSql2.Append("select count(userId) FROM UserInfo");                    strSql2.AppendLine(" where deptId=" + int.Parse(dt1.Rows[i]["deptId"].ToString()));                    object objDeptStaffNum = DbHelperSQL.GetSingle(strSql2.ToString());                    countStaffNum += int.Parse(objDeptStaffNum.ToString());                    countStaffNum += GetStaffCountByUpLeaderId(int.Parse(dt1.Rows[i]["deptId"].ToString()), 0);//统计下级部门员工(递归)                }            }            return countStaffNum;        }


[解决办法]
countStaffNum 提升为全局变量
C# code
int countStaffNum = 0;     public static int GetStaffNumByUpLeaderId(int UpleaderId, int parentDeptId)        {                        StringBuilder strSql1 = new StringBuilder();            if (UpleaderId > 0)            {                strSql1.Append("select deptId,parentDeptId,deptUpLeaderId FROM Department");                strSql1.Append(" where parentDeptId=" + parentDeptId);                strSql1.Append(" and deptUpLeaderId=" + UpleaderId);            }            else            {                strSql1.Append("select deptId FROM Department");                strSql1.Append(" where parentDeptId=" + parentDeptId);            }            DataTable dt1 = DbHelperSQL.Query(strSql1.ToString()).Tables[0];            if (dt1.Rows.Count>0)            {                for (int i = 0; i < dt1.Rows.Count; i++)                {                    StringBuilder strSql2 = new StringBuilder();                    strSql2.Append("select count(userId) FROM UserInfo");                    strSql2.AppendLine(" where deptId=" + int.Parse(dt1.Rows[i]["deptId"].ToString()));                    object objDeptStaffNum = DbHelperSQL.GetSingle(strSql2.ToString());                    countStaffNum += int.Parse(objDeptStaffNum.ToString());                    countStaffNum += GetStaffCountByUpLeaderId(int.Parse(dt1.Rows[i]["deptId"].ToString()), 0);//统计下级部门员工(递归)                }            }            return countStaffNum;        } 

读书人网 >C#

热点推荐