Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 遍历子对象到父对象_C#_Asp.net_Entity Framework_Asp.net Core - Fatal编程技术网

C# 遍历子对象到父对象

C# 遍历子对象到父对象,c#,asp.net,entity-framework,asp.net-core,C#,Asp.net,Entity Framework,Asp.net Core,我正在从以下查询中提取数据 var targetItems = await _dbContext.TargetItems .Include(ti => ti.BranchTargetItem) .ThenInclude(bti => bti.BranchTarget) .ToListAsync(cancellationToken); 我需要在我的树中找到一种从下到上求和的方法。必须分别对其自身的第一级子值求和,如我在下面注释的

我正在从以下查询中提取数据

    var targetItems = await _dbContext.TargetItems
        .Include(ti => ti.BranchTargetItem)
        .ThenInclude(bti => bti.BranchTarget)
        .ToListAsync(cancellationToken);
我需要在我的树中找到一种从下到上求和的方法。必须分别对其自身的第一级子值求和,如我在下面注释的

targetItems = {
   "item":"A",
   "values":14, << sum of B + C values
   "childs":[
      {
         "item":"B",
         "values":2,
         "childs":[]
      },
      {
         "item":"C", << sum of D + E + H values
         "values":12,
         "childs":[
            {
               "item":"D",
               "values":5,
               "childs":[]
            },
            {
               "item":"E",
               "values":4, << sum of F + G values
               "childs":[
                  {
                     "item":"F",
                     "values":2,
                     "childs":[]
                  },
                  {
                     "item":"G",
                     "values":2,
                     "childs":[]
                  }
               ]
            },
            {
               "item":"H",
               "values":3,
               "childs":[]
            }
         ]
      }
   ]
}
targetItems={
“项目”:“A”,

“价值观”:14,您至少可以发布包含这些项的类,还是需要解析/反序列化的json?@ZoharPeled我的类与示例json非常接近,我只是简化了示例输出以便于理解。每个节点都是相同的类型吗?如果是,您可以递归。@JohnathanBarclay是的,每个节点都是完全相同的类型。您能在east post是保存这些项的类,还是您需要解析/反序列化的json?@ZoharPeled我的类与示例json非常接近,我只是简化了示例输出以便于理解。每个节点都是相同的类型吗?如果是,您可以递归。@JohnathanBarclay是的,每个节点都是完全相同的类型。它工作正常,谢谢非常感谢。递归方法真的是一个很好的实践。它按预期工作,非常感谢。递归方法真的是一个很好的实践。
int SetSumOfChildren(object node){
  if(!(node.childs?.Any() ?? false)){
    node.values = node.values ?? 0;
  }
  else {      
    int sum = 0;
    foreach(var n in node.childs){
      sum += SetSumOfChildren(n);
    }
    node.values = sum;
  }
  return node.values;
}

SetSumOfChildren(data);