父级中子级值的总和 [英] Sum of child values in parent

查看:94
本文介绍了父级中子级值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有下表:

I have the following table in my database:

ID      IsLeaf     Description     Parent_ID     value
1--------0 ------------11--------------0-------------0
2--------0 ------------22--------------0-------------0
3--------1 ------------110001----------1-------------100
4--------0 ------------2202------------2-------------0
5--------1 ------------22020001--------4-------------159
6--------1 ------------110002----------1-------------40



我希望孩子的总数在其父级中,并且当父级也是另一个父级的子级时,则父级的总和也应在其父级中.

当我检索数据时,我想在DataTable中得到它.我使用Winforms和C#.



I want the sum of the children to be in its parent, and when the parent is also a child of another parent, then the sum of the parents should be in its parent.

When I retrieve data I want to get it like this in a DataTable. I use Winforms and C#.

ID      IsLeaf     Description     Parent_ID     value
1--------0 ------------11--------------0-------------140
2--------0 ------------22--------------0-------------159
3--------1 ------------110001----------1-------------100
4--------0 ------------2202------------2-------------159
5--------1 ------------22020001--------4-------------159
6--------1 ------------110002----------1-------------40



这一些注意事项:
1-IsLeaf栏:
如果1为"true",则表示该记录可以带有值,而不能成为该值的子代.
如果0为"false",则表示该记录不能取值,可以使其成为子记录.
但在dataTable中,父记录具有其子项的总和

2-如果解决方案需要任何编辑"列(添加"或删除"),那就没问题.

我需要解决方案

我感谢您的任何答复或建议.谢谢.



this some Notes:
1-IsLeaf column:
if 1 "true" that''s mean the Record can take value and can not make child of it.
if 0 "false" that''s mean the Record can not take value and can make child of it.
but in dataTable the Parent Record Has The Sum Of Its Child

2-If the solution Need any Edit In columns (Add or Remove) thats not problem.

i need solution

I appreciate any replies or advice. Thank you.

推荐答案

让我们尝试一下

lets try something

void iteratparents(){
  foreach (DataRow parentrecord in mydatatable.Select("Parent_ID=0")
    setparentvalue(parentrecord);
}

void setparetrecord(DataRow parentrecord){
  if (!((bool) parentrecord["IsLeaf"])) {
    int sum = 0;
    DataRow[] children = mydatatable.Select("Parent_ID=" + (string) parentrecord["ID"] );
    foreach (DataRow childrecord in children){
      setparetrecord(childrecord);
      sum += (int) childrecord["value"];
    }

    parentrecord["value"] = sum;
  }
}



祝你好运;

100:rose:



Good Luck;

100 :rose:


这篇关于父级中子级值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆