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

相关推荐
爱上语文1 小时前
Redis基础(4):Set类型和SortedSet类型
java·数据库·redis·后端
lifallen1 小时前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
Brookty2 小时前
【MySQL】JDBC编程
java·数据库·后端·学习·mysql·jdbc
先做个垃圾出来………2 小时前
SQL的底层逻辑解析
数据库·sql
码不停蹄的玄黓3 小时前
深入拆解MySQL InnoDB可重复读(RR)隔离级别:MVCC+临键锁如何「锁」住一致性?
数据库·mysql·可重复读
paopaokaka_luck3 小时前
基于SpringBoot+Vue的酒类仓储管理系统
数据库·vue.js·spring boot·后端·小程序
薛晓刚4 小时前
哪个领域数据库最难替换?
数据库
芷栀夏4 小时前
基于Anything LLM的本地知识库系统远程访问实现路径
数据库·人工智能
软件2055 小时前
【redis使用场景——缓存——数据淘汰策略】
数据库·redis·缓存
ChinaRainbowSea5 小时前
9-2 MySQL 分析查询语句:EXPLAIN(详细说明)
java·数据库·后端·sql·mysql