当然实体不一定叫tree,Child也不一定叫Child, ParentId也不一定叫Parentid,结构上一样就行
//第三个参数为0 表示 : parentid为 0 设置成根目录, 正常情况下最顶层的parentid不是0就是null
var tree = db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0);
//异步用ToTreeAsync
//实体
public class Tree
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; } //关联字段 默认是主键
public string Name { get; set; }
public int ParentId { get; set; }//父级字段
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<Tree> Child { get; set; }
}
//无主键用法新:5.1.4.110
db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
//List直接构造树 要高版本sqlsugar
UtilMethods.BuildTree(db,list, pk, parentIdName, childName, rootValue)5.1.3.28 正常情况下根据Id过滤会因为没有ParentId构造不了树,SqlSugar内部处理过了
//主键过滤
object [] inIds=new object[]{11,12}
db.Queryable<Tree2>().ToTree(it => it.Child, it => it.ParentId, 0,inIds)
//影响构造树的条件过滤
object [] inIds=db.Queryable<Tree2>()
.Where(it=>it.name.Contains("a"))
.Select(it=>it.Id).ToList().Cast<object>().ToArray();
//将条件过滤后的ID传进ToTree
db.Queryable<Tree2>().ToTree(it => it.Child, it => it.ParentId, 0,inIds)
//如果where不影响树的构造可以直接用Where
db.Queryable<Tree2>().Where(it=>it.Type==1).ToTree(it => it.Child, it => it.ParentId, 0,inIds)获取主键为11的所有上级对象
db.Queryable<Tree>().ToParentList(it=>it.ParentId,11); //这个方法只支持最简单的单表查询
//新功能 5.0.7.9-preview05
//关联字段如果不是主键我们可以
//设置特性 SugarColumn(IsTreeKey=true)
// public string Code{get;set;}上的ToTree是有层级的,ToChildList是没层级的
var allchilds2= db.Queryable<Tree>().ToChildList(it => it.ParentId, 2);//2是主键,查主键为2下面所有
//新功能 5.0.7.9-preview05
//关联字段如果不是主键我们可以
//设置特性 SugarColumn(IsTreeKey=true)
// public string Code{get;set;}新功能 :5.0.7.9-preview05
如果不是主键关联 , 设置特性 SugarColumn(IsTreeKey=true)
//方式1:指定id和parentid
db.Queryable<Tree>().ToTree(it => it.Child, it => it.ParentCode, 0,it=>it.Code)
//方式2: 通过特性指定id ,支持的功能多ToChildList等都能用
db.Queryable<Tree2 >().ToTree(it=>it.Child,it=>it.ParentCode,0)
public class Tree2
{
[SqlSugar.SugarColumn(IsPrimaryKey =true)]
public int Id { get; set; }
public string Name { get; set; }
[SugarColumn(IsTreeKey=true)] //设置关联字段
public string Code{get;set;}
public string ParentCode{ get; set; } //父级字段
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<Tree2 > Child { get; set; } //这边也是tree2
}如果没有载重载要升级 5.1.4.110 或者 给DTO加主键
db.Queryable<Tree>()
.LeftJoin<Table>((it,t)=>it.TreeId==t.Id)
.Select((it,t)=>new ViewModel(){
ParentId=it.ParentId,
Id=it.Id,
TName=t,Name
})
.ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载当注意:下面是ParentId和主键关联的例子(如果不是主键看导航查询 2.4 结合Toree非主键例子)
var list=db.Queryable<Tree1>().Includes(x => x.Parent)
.ToTree(x => x.Child, x => x.ParentId, 0);
public class Tree1
{
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
[Navigate(NavigateType.OneToOne,nameof(ParentId))]//设置导航 一对一
public Tree1 Parent { get; set; }
[SugarColumn(IsIgnore =true)]
public List<Tree1> Child { get; set; }
}如果没有载重载要升级 5.1.4.110 或者 给DTO加主键
var list4 = await db.Queryable<MaterialTree>()
.Includes(p => p.Parent)//查上级
.LeftJoin<MaterialInformation>((t1, t2) => t1.PartCode == t2.PartCode)
.Select((t1, t2) => new MaterialDto()
{
PartCode = t1.PartCode,
ParentPartCode = t1.ParentPartCode,
item1 = t2.item1,//通过联表拿字段
item2 = t2.item2,
Parent = t1.Parent//通过导航拿到上级
})
.ToTreeAsync(p => p.Children, p => p.ParentPartCode, 0, p => p.PartCode);2016 © donet5.comApache Licence 2.0