业务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中内容文章来源:https://www.toymoban.com/news/detail-727059.html
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文章来源地址https://www.toymoban.com/news/detail-727059.html
到了这里,关于【.net core】yisha框架 SQL SERVER数据库 反向递归查询部门(子查父)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!