簡體   English   中英

查找最小日期/用途和最大日期/用途

[英]finding the min date/usage and max date/usage

我需要一些幫助。 我有一個包含多個相同節點的XML帳單。 現在,im遍歷節點並相應地合並值。 我向您顯示的代碼只是我需要幫助的部分。 循環包含許多我將值合並在一起的地方。 我需要幫助的是選擇MIN和MAX日期/用法。 由於我現在返回多個節點而不是一個,因此我將循環並合並數據。

  1. 因此,在遍歷節點之后,我需要將UsageMeterReadStartDate設置為最早的MIN日期。 dataType字符串
  2. 與UsageMeterReadStartUsage相同。 dataType字符串
  3. 因此,在遍歷節點之后,我需要將UsageMeterReadStartDate設置為最新的MAX日期。 dataType字符串
  4. 與UsageMeterReadEndUsage相同

那就是我所堅持的。 我已經完成了我需要的所有其他數據的合並。 我今年18歲,是一位相當新的程序員。 我只是迷失了自己的邏輯。 任何指導都會幫助我。 我相信最后兩個IF語句是我需要幫助的地方以及邏輯的去向。

我認為您想做的是首先遍歷所有項目。 將它們轉換為DateTime對象。 DateTime對象使您可以比較大於或小於的其他對象。 但我認為,您會陷入年度過渡,因為您沒有在源中進行跟蹤。

if (meterReadStartXMLNodes.Count > 0 && meterReadStartXMLNodes[0].HasChildNodes)
{   // This is to fill up the meter read start date and meter read start usage as an attribute of the "sadetails" node in the newer XML. 
    DateTime oldDateTime = DateTime.Now;
    string oldUsage = "";
    foreach (var node in meterReadStartXMLNodes)
    {
        DateTime tempDateTime = DateTime.Parse(String.Format("{0} {1}", meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_STRT_DT_MM.USAGE").InnerText,
                                                                        meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_STRT_DT_DD.USAGE").InnerText));

        if (tempDateTime < oldDateTime)
        {
            // Any time you've determined you have an older date, we capture the usage for that date
            oldDateTime = tempDateTime;
            oldUsage = meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_KWH_PRV_MTR_READ.USAGE").InnerText;
        }
    }

    // By the time you get here, you've already determined the lowest date/time, format it.
    saBillDetail.UsageMeterReadStartDate = oldDateTime.ToString("MMM dd");
    saBillDetail.UsageMeterReadStartUsage = oldUsage;
}

對於結束日期,您可以執行相同的操作,除了不需要為其分配值,默認值將類似於0001年。

if (meterReadEndXMLNodes.Count > 0 && meterReadEndXMLNodes[0].HasChildNodes)
{   // This is to fill up the meter read end date and meter read end usage as an attribute of the "sadetails" node in the newer XML.

    DateTime latestDateTime = new DateTime();
    string latestUsage = "";
    foreach (var node in meterReadStartXMLNodes)
    {
        DateTime tempDateTime = DateTime.Parse(String.Format("{0} {1}", meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_END_DT_MM.USAGE").InnerText,
                                                                        meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_USG_END_DT_DD.USAGE").InnerText));

        if (tempDateTime > latestDateTime)
        {
            // Any time you've determined you have an newer date, we capture the usage for that date
            latestDateTime = tempDateTime;
            latestUsage = meterReadStartXMLNodes[0].SelectSingleNode("IRBILGU_US_KWH_MTR_READ.USAGE").InnerText;
        }
    }

    saBillDetail.UsageMeterReadEndDate = latestDateTime.ToString("MMM dd");
    saBillDetail.UsageMeterReadEndUsage = latestUsage;
}

當然,我沒有測試這些東西,但是它應該可以幫助您解決這些錯誤。 既然您是編程新手,您是否了解我的工作?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM