Rendering a hierarchy using LINQ?

前端 未结 6 1029
终归单人心
终归单人心 2020-12-10 08:05

Let say we have a class

Category
{
   ID,
   Name,
   ParentID
}

and a List

1, \'Item 1\', 0
2, \'Item 2\', 0
3, \'Item 3\'         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 08:44

       @model List
       @{    
    
        Func, string> recuresive = null;
    recuresive = (parentid, list) => string.Join("", list.Where(x => x.ParentId == parentid).Select(x => "
  • " + x.Name + "
      " + recuresive(x.Id, list.Where(y => y.ParentId != parentid).ToList()) + "
  • ")); } @Html.Raw("
      " + recuresive(null, Model) + "
    ")

提交回复
热议问题