如何使用LINQ从XML获得子节点值的总和

时间:2018-07-29 11:02:21

标签: linq

    <G_PERSON>
          <SORT_PERSON_DISP>Banking &amp; Finance / Capital Markets Group (B&amp;F)</SORT_PERSON_DISP>
          <G_DEBTOR>
            <TAG_DEBTOR_CODE>204342-21-55</TAG_DEBTOR_CODE>
            <DEBTOR_NAME>ENGIE</DEBTOR_NAME>
            <DEBTOR_STATUS></DEBTOR_STATUS>
            <LIST_G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510038681</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40676709</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>ENGIE - financial regulatory advice</TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>26-APR-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>0</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>9712.5</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>0</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>9712.5</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>9712.5</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>9712.5</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
            </LIST_G_IVS_INVOICE_USER_REF>
          </G_DEBTOR>
          <G_DEBTOR>
            <TAG_DEBTOR_CODE>219271-21-55</TAG_DEBTOR_CODE>
            <DEBTOR_NAME>Leaseplan Corporation N.V.</DEBTOR_NAME>
            <DEBTOR_STATUS>Normal</DEBTOR_STATUS>
            <LIST_G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510038829</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40674499</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>LeasePlan - MiFID2 implementation </TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>11-MAY-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>0</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>11301.1</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>0</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>11301.1</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>11301.1</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>11301.1</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510038863</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40671502</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>Leaseplan - MTN Programme 2018</TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>18-MAY-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>0</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>4274.71</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>0</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>4274.71</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>4274.71</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>4274.71</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510038909</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40683087</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>LP NZ - HSBC guarantee legal opinion</TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>28-MAY-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>3308.07</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>0</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>0</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>3308.07</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>3308.07</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>3308.07</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
            </LIST_G_IVS_INVOICE_USER_REF>
          </G_DEBTOR>

          </G_PERSON>
      </G_BREAK>
      <G_BREAK>
        <G_PERSON>
          <SORT_PERSON_DISP>Banking &amp; Finance / Finance Group</SORT_PERSON_DISP>
          <G_DEBTOR>
            <TAG_DEBTOR_CODE>217626-21-55</TAG_DEBTOR_CODE>
            <DEBTOR_NAME>Blue Sky Group Holding B.V.</DEBTOR_NAME>
            <DEBTOR_STATUS>Normal</DEBTOR_STATUS>
            <LIST_G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510037763</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40523323</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>Regulatory and other issues</TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>25-JAN-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>0</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>0</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>1091.11</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>1091.11</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>1091.11</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>1091.11</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510038528</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40523323</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>Regulatory and other issues</TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>13-APR-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>0</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>3938.37</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>0</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>3938.37</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>3938.37</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>3938.37</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
            </LIST_G_IVS_INVOICE_USER_REF>
          </G_DEBTOR>
                   <G_DEBTOR>
            <TAG_DEBTOR_CODE>217965-21-55</TAG_DEBTOR_CODE>
            <DEBTOR_NAME>Circle Holding Netherlands B.V.</DEBTOR_NAME>
            <DEBTOR_STATUS>Normal</DEBTOR_STATUS>
            <LIST_G_IVS_INVOICE_USER_REF>
              <G_IVS_INVOICE_USER_REF>
                <IVS_INVOICE_USER_REF>215510038925</IVS_INVOICE_USER_REF>
                <G_PROJECT>
                  <TAG_PRJ_NUMBER>40682439</TAG_PRJ_NUMBER>
                  <TAG_PRJ_OFF_OFFICE>55</TAG_PRJ_OFF_OFFICE>
                  <TAG_PRJ_DESC>Circle AFM SEC</TAG_PRJ_DESC>
                  <G_INVOICE>
                    <IVS_INVOICE_DATE>29-MAY-18</IVS_INVOICE_DATE>
                    <CS_TAG_DEBTOR_SLOT_1>1529.9</CS_TAG_DEBTOR_SLOT_1>
                    <CS_TAG_DEBTOR_SLOT_2>0</CS_TAG_DEBTOR_SLOT_2>
                    <CS_TAG_DEBTOR_SLOT_3>0</CS_TAG_DEBTOR_SLOT_3>
                    <CS_TAG_DEBTOR_SLOT_4>0</CS_TAG_DEBTOR_SLOT_4>
                    <CS_TAG_DEBTOR_SLOT_5>0</CS_TAG_DEBTOR_SLOT_5>
                    <CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>1529.9</CS_IVS_DEBTOR_TOTAL_VALUE_ORIG>
                    <CS_PROVISIONAL_WOFF>0</CS_PROVISIONAL_WOFF>
                    <CS_CF_TOTAL_DEBTORS_NET>1529.9</CS_CF_TOTAL_DEBTORS_NET>
                    <CS_CF_TOTAL_DEBTORS>1529.9</CS_CF_TOTAL_DEBTORS>
                  </G_INVOICE>
                </G_PROJECT>
              </G_IVS_INVOICE_USER_REF>
            </LIST_G_IVS_INVOICE_USER_REF>
          </G_DEBTOR>
               </G_PERSON>
      </G_BREAK>

在此XML中,我想将SORT_PERSON_DISP元素的CS_CF_TOTAL_DEBTORS_NET元素值求和

 var debt=  from K_PA in new_keystone.Descendants().Elements("G_PERSON").Elements("SORT_PERSON_DISP")//.First().Value//.Substring(0, new_keystone.Descendants().Elements("G_PERSON").Elements("SORT_PERSON_DISP").First().Value.IndexOf("/"))

             from KeystoneRpt in new_keystone.Descendants().Elements("G_PERSON").GroupBy(grpby => grpby.Elements("SORT_PERSON_DISP").First().Value.Substring(0, grpby.Element("SORT_PERSON_DISP").Value.IndexOf("/")))  //group MIRA by MIRA.Attributes("Textbox9").First().Value // into PAGroupMIRA
                    .Select(grp => new { SumCash = grp.Sum(o => double.Parse(o.Descendants().Elements("CS_CF_TOTAL_DEBTORS_NET").First().Value)) })

                       select new ResultEntity

                       {
                           Results = new Dictionary<string, string>
                              {
                               {"PA",K_PA.Value.Substring(0, K_PA.Value.IndexOf("/")) },
                               // {"PA",K_PA.Elements("SORT_PERSON_DISP").First().Value.Substring(0, new_keystone.Descendants().Elements("G_PERSON").Elements("SORT_PERSON_DISP").First().Value.IndexOf("/"))  },
                               {"debt",Convert.ToString(KeystoneRpt.SumCash )}
                           }
                       };

           CommonFunctions.WriteCSV(debt, "060andmira");
        }

此代码有效,但仅采用SORT_PERSON_DISP的第一个值。

0 个答案:

没有答案