【.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

相关推荐
IT 小阿姨(数据库)4 分钟前
PgSQL中pg_stat_user_tables 和 pg_stat_user_objects参数详解
linux·运维·数据库·sql·postgresql·oracle
倔强的石头_14 分钟前
Windows系统下KingbaseES数据库保姆级安装教程(附常见问题解决)
数据库
麦兜*1 小时前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
RestCloud1 小时前
PostgreSQL大表同步优化:如何避免网络和内存瓶颈?
前端·数据库·api
阿里云大数据AI技术1 小时前
淘宝闪购基于Flink&Paimon的Lakehouse生产实践:从实时数仓到湖仓一体化的演进之路
数据库·flink
努力学习的小廉2 小时前
深入了解linux系统—— 线程同步
linux·服务器·数据库·算法
格调UI成品2 小时前
DCS+PLC协同优化:基于MQTT的分布式控制系统能效提升案例
数据库·云边协同
牵牛老人3 小时前
Qt C++ 复杂界面处理:巧用覆盖层突破复杂界面处理难题之一
数据库·c++·qt
GBASE3 小时前
GBASE南大通用技术分享:构建最优数据平台,GBase 8s数据库安装准备(三)
数据库
言之。3 小时前
Django REST Framework 中 @action 装饰器详解
数据库·sqlite