【.net core】yisha框架 SQL SERVER数据库 反向递归查询部门(子查父)

业务service.cs中ListFilter方法中内容

cs 复制代码
//反向递归查询部门列表
                    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中内容

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需为单个ID
            strSql.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

相关推荐
吱吱鼠叔31 分钟前
MATLAB数据文件读写:1.格式化读写文件
前端·数据库·matlab
小哇66634 分钟前
spring-TransactionTemplate 编程式事务
数据库·spring
如意机反光镜裸1 小时前
CentOS7安装MySQL教程
数据库·mysql
冰镇毛衣1 小时前
1.4 MySql配置文件
数据库·mysql
攻城狮的梦1 小时前
redis集群模式连接
数据库·redis·缓存
标贝科技2 小时前
ChatGPT对话训练数据采集渠道有哪些
数据库·人工智能·机器学习·chatgpt
乌啼霜满天2492 小时前
如何将MySQL卸载干净(win11)
数据库·mysql
2的n次方_2 小时前
掌握Spring Boot数据库集成:用JPA和Hibernate构建高效数据交互与版本控制
数据库·spring boot·hibernate
NaZiMeKiY3 小时前
SQLServer数据分页
数据库·sql·sqlserver
Python私教3 小时前
Python国产新 ORM 框架 fastzdp_sqlmodel 快速入门教程
java·数据库·python