繁体   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