问题
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