已解决
【.net core】yisha框架 SQL SERVER数据库 反向递归查询部门(子查父)
来自网友在路上 11048104提问 提问时间:2023-10-10 02:32:56阅读次数: 104
最佳答案 问答题库1048位专家为你答疑解惑
业务service.cs中ListFilter方法中内容
//反向递归查询部门列表List<DepartmentEntity> departmentList = await departmentService.GetReverseRecurrenceList(new DepartmentListParam() { Ids = operatorInfo.DepartmentId.ToString() });if (departmentList != null && departmentList.Count > 0){//查找列表中为根节点的部门DepartmentEntity department = departmentList.Find(a => a.ParentId == 0);if (department != null){strSql.Append(" AND a.AffiliatedUnit = @AffiliatedUnit");parameter.Add(DbParameterExtension.CreateDbParameter("@AffiliatedUnit", department.Id));}}
DepartmentService.cs中内容
public async Task<List<DepartmentEntity>> GetReverseRecurrenceList(DepartmentListParam param){var strSql = new StringBuilder();List<DbParameter> filter = ListFilter(param, strSql);var list = await this.BaseRepository().FindList<DepartmentEntity>(strSql.ToString(), filter.ToArray());return list.ToList();}private List<DbParameter> ListFilter(DepartmentListParam param, StringBuilder strSql, bool bNewsContent = false){//param.ids需为单个IDstrSql.Append($@"with temp as ( select * from SysDepartment where id = {(!string.IsNullOrEmpty(param.Ids) ? param.Ids : "0")}union all select a.* from SysDepartment a inner join temp on temp.[parentId] = a.[id]) select * from temp");var parameter = new List<DbParameter>();//strSql.Append(@" ORDER BY cast(ISNULL(BuildingNumber, '0') as int) asc,cast(ISNULL(Floor, '0') as int) asc,HouseCode asc ");return parameter;}
mysql数据库中参考mysql 递归语法修改sql
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"【.net core】yisha框架 SQL SERVER数据库 反向递归查询部门(子查父)":http://eshow365.cn/6-18116-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!