How to write the Looping structure for Parent-Child relationships?

江枫思渺然 提交于 2019-12-11 05:44:49

问题


I am using treetable.js. as procedure i saved the child id and parent id. insert data is successfully inserted. but return data may show in table only or get the grouping error. then i use the IGrouping Object get the Convertion error. Please tell me the child parent loop structure for the below.

In Model:

public string name {get;set;}    
public int childId {get;set;}
public int  ParentId {get;set;}

In Controller:

var list = db.table.groupby(s=>s.parentId).toList();
   return view(list);

In view:

 <table id="example-basic">
         <tr>

             <th>Name</th>
             <th>Data Id</th>
             <th>Parent Id</th>
         </tr>
     @foreach (var item in Model)
     {
        <tr data-tt-id='@item.dataid' data-tt-parent-id='@item.dataparentid'>

            <td>@item.Name</td>
            <td>@item.dataid</td>
            <td>@item.dataparentid</td>
        </tr>
     }
 </table>

回答1:


If you want just get rid of exception with grouping you should write like this:

  var list = db.table.groupby(s => s.parentId).Select(x => new ChartOfAccount 
  { 
       ParentId = x.Key,
       childId = x.First().childId,
       name = x.First().name
  }).toList();
  return view(list);

As you can see you will get only one row for each ParentId becouse or grouping.

But i gess you searching for something else.



来源:https://stackoverflow.com/questions/31285423/how-to-write-the-looping-structure-for-parent-child-relationships

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!