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

相关推荐
shepherd枸杞泡茶4 小时前
第3章 3.3日志 .NET Core日志 NLog使用教程
c#·asp.net·.net·.netcore
山猪打不过家猪4 小时前
ASP.NET Core Clean Architecture
java·数据库·asp.net
csdn_aspnet4 小时前
ASP.NET Core 简单文件上传
asp.net·.netcore
qwy7152292581635 小时前
13-R数据重塑
服务器·数据库·r语言
Bio Coder5 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言
钊兵6 小时前
数据库驱动免费下载(Oracle、Mysql、达梦、Postgresql)
数据库·mysql·postgresql·oracle·达梦·驱动
weixin_425878237 小时前
Redis复制性能优化利器:深入解析replica-lazy-flush参数
数据库·redis·性能优化
左灯右行的爱情7 小时前
Redis数据结构总结-listPack
数据结构·数据库·redis
隔壁老王1568 小时前
mysql实时同步到es
数据库·mysql·elasticsearch