简体   繁体   English

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

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

I am in need of some assistance. 我需要一些帮助。 I have an XML bill that has multiple same Nodes. 我有一个包含多个相同节点的XML帐单。 right now im looping through the nodes and merging the values accordingly. 现在,im遍历节点并相应地合并值。 The code I am showing you is only the part i need help with. 我向您显示的代码只是我需要帮助的部分。 The loop contains a lot more where i am merging values together. 循环包含许多我将值合并在一起的地方。 What I need help is with choosing the MIN and MAX dates/usages. 我需要帮助的是选择MIN和MAX日期/用法。 Since I am returning multiple nodes instead of one now i will loop and merge the data. 由于我现在返回多个节点而不是一个,因此我将循环并合并数据。

  1. So after it loops through the nodes i need the UsageMeterReadStartDate to be set to the earliest MIN date. 因此,在遍历节点之后,我需要将UsageMeterReadStartDate设置为最早的MIN日期。 dataType string dataType字符串
  2. Same thing with UsageMeterReadStartUsage. 与UsageMeterReadStartUsage相同。 dataType string dataType字符串
  3. So after it loops through the nodes i need the UsageMeterReadStartDate to be set to the latest MAX date. 因此,在遍历节点之后,我需要将UsageMeterReadStartDate设置为最新的MAX日期。 dataType string dataType字符串
  4. same with UsageMeterReadEndUsage 与UsageMeterReadEndUsage相同

That is what I am stuck on. 那就是我所坚持的。 I have already finished merging all of the other data i need. 我已经完成了我需要的所有其他数据的合并。 I am 18yrs old and a pretty new programmer. 我今年18岁,是一位相当新的程序员。 I'm just getting lost in my own logic. 我只是迷失了自己的逻辑。 Any guidance would help me out. 任何指导都会帮助我。 I beleive The last two IF statements is where i need help and where the logic would go. 我相信最后两个IF语句是我需要帮助的地方以及逻辑的去向。

I think what you want to do is loop through all of the items first. 我认为您想做的是首先遍历所有项目。 Convert these to a DateTime object. 将它们转换为DateTime对象。 The DateTime object lets you compare others for greater than or less than. DateTime对象使您可以比较大于或小于的其他对象。 I think though, you'll get caught up in year transitions because you're not tracking that in the source. 但我认为,您会陷入年度过渡,因为您没有在源中进行跟踪。

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;
}

For the end dates you sort of do the same thing, except don't need to assign it a value, it will default to like year 0001. 对于结束日期,您可以执行相同的操作,除了不需要为其分配值,默认值将类似于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;
}

Granted, I didn't test this stuff, but it should get you going and you can fix the bugs. 当然,我没有测试这些东西,但是它应该可以帮助您解决这些错误。 Since you're new to programming, do you understand what I've done? 既然您是编程新手,您是否了解我的工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM