locked
Get Column Value of Next Row in foreach Loop RRS feed

  • Question

  • User-972094631 posted

    Hello,

    In this scenario there can be multiple output for the same item if there are different start dates for an item.

    I am looping through response items in XML output and need to be able to grab the next value and perform a calculation as follows:

    (If there is only one line item (or if it is the last item) returned, end date = '12/31/2099' else end date = <start_date> on next line item – 1 day)

    Here is the XML output for an item with 2 start dates:

      <rqsts>
        <rqstID>123</rqstID>
        <rqstMsg/>
      </rqsts>
      <items>
        <item>
          <startDate>2014-07-25</startDate>
          <itmTest>123123123123</itmTest>
          <prcAmt>00003.3100</prcAmt>
          <rc/>
          <itmMsg/>
          <cmps>
            <cmp>
              <cmpNbr>0010</cmpNbr>
              <cmpName>VALUE1</cmpName>
              <cmpAmt>0003.1300</cmpAmt>
            </cmp>
            <cmp>
              <cmpNbr>0500</cmpNbr>
              <cmpName>VALUE2</cmpName>
              <cmpAmt>0000.1800</cmpAmt>
            </cmp>
            <cmp>
              <cmpNbr>0410</cmpNbr>
              <cmpName>VALUE3</cmpName>
              <cmpAmt>0003.3100</cmpAmt>
            </cmp>
          </cmps>
        </item>
        <item>
          <startDate>2014-07-26</startDate>
          <itmTest>123123123123</itmTest>
          <prcAmt>00003.3100</prcAmt>
          <rc/>
          <itmMsg/>
          <cmps>
            <cmp>
              <cmpNbr>0010</cmpNbr>
              <cmpName>VALUE1</cmpName>
              <cmpAmt>0005.1300</cmpAmt>
            </cmp>
            <cmp>
              <cmpNbr>0500</cmpNbr>
              <cmpName>VALUE2</cmpName>
              <cmpAmt>0000.2800</cmpAmt>
            </cmp>
            <cmp>
              <cmpNbr>0410</cmpNbr>
              <cmpName>VALUE3</cmpName>
              <cmpAmt>0023.3100</cmpAmt>
            </cmp>
          </cmps>
    </item> </items> </rqst>


    Here is the C# code-behind which passes values to and returns from the web service:

    	DataTable GetTestDetails()
            {                       
                string item;
                item = _providerInfo.ItemCode;
    
                DataTable dtItem = new DataTable();
                dtItem = oTest.GetTestDetails(item);
    
                DateTime effectiveDate = DateTime.Today.Date;
                          
    	    DataTable dtPricingInfo = new DataTable();
                dtPricingInfo.Clear();         
               
                List<ResponseItem> outputXml = oTest.GetItemCostSellService(
                                   item,                                                                              
                                   effDate);
    
                dtPricingInfo.Columns.Add("VALUE1", typeof(String));
                dtPricingInfo.Columns.Add("VALUE2", typeof(String));
                dtPricingInfo.Columns.Add("VALUE3", typeof(String));
    
    //If there is only one line item (or last item) returned, end date = '12/31/2099' else end date = <start_date>
    //on next line item in XML output – 1 day.   DateTime endDate;
    endDate = Convert.ToDateTime(String.Format("{0:M/d/yyyy}", Convert.ToDateTime(effDate).AddDays(-1))); dtPricingInfo.Columns.Add("ENDDATE", typeof(DateTime)); foreach (ResponseItem itm in outputXml) { DataRow dr = dtPricingInfo.NewRow();
    if (itm.Components.ToList().Find(ic => ic.Name == "VALUE1") != null)
    dr["VALUE1"] = itm.Components.ToList().Where(ic => ic.Name == "VALUE1").FirstOrDefault().Amount;
    if (itm.Components.ToList().Find(ic => ic.Name == "VALUE2") != null)
    dr["VALUE2"] = itm.Components.ToList().Where(ic => ic.Name == "VALUE2").FirstOrDefault().Amount;
    if (itm.Components.ToList().Find(ic => ic.Name == "VALUE3") != null)
    dr["VALUE3"] = itm.Components.ToList().Where(ic => ic.Name == "VALUE3").FirstOrDefault().Amount;
    dr["ENDDATE"] = endDate;
    dtPricingInfo.Rows.Add(dr); } dtPricingInfo.Rows.Add(); return dtPricingInfo; }

    Any help would be greatly appreciated.


    Thanks,

    Buster

    Wednesday, August 6, 2014 7:36 PM

Answers

  • User465171450 posted

    Can't do it. Instead though, why not just a for loop? Then you can do something like:

    for(int i = 0 ; i < outputXml.Count;i++)
    {
    // get the current item
       ResponseItem itm = outputXml[i];
       // get the next item
       ResponseItem nextitm = null;
       if(i < (outputXml.Count -1)
       { nextitm = outputXml[i];}
       
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 7, 2014 12:56 AM