如何写父子关系的循环结构? [英] How to write the Looping structure for Parent-Child relationships?
问题描述
我使用treetable.js。作为程序我救孩子ID和父ID。插入数据成功插入。但是返回的数据可能表只显示或获取分组错误。
然后我用IGrouping对象得到皈依错误。
请告诉我孩子父母循环结构的下方。
在模型:
公共字符串名称{;设置;}
公众诠释childID的{获取;设置;}
公众诠释的ParentId {获取;设置;}
在控制器:
VAR列表= db.table.groupby(S = GT; s.parentId).toList();
返回视图(名单);
鉴于:
<表ID =例如,基本>
&所述; TR> <第i个姓名和LT; /第i
<第i个数据ID< /第i
<第i个父ID和LT; /第i
< / TR>
@foreach(以型号VAR项)
{
&所述; 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>
}
< /表>
如果你只想得到与分组,你应该这样写摆脱的异常:
VAR列表= db.table.groupby(S = GT; s.parentId)。选择(X =>新建ChartOfAccount
{
的ParentId = x.Key,
childID的= x.First()。childID的,
名称= x.First()。名称
})了ToList()。
返回视图(名单);
正如你可以看到你将获得唯一的每一行的ParentId
becouse或分组。
但我GESS您要找的是别的东西。
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>
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.
这篇关于如何写父子关系的循环结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!