简体   繁体   中英

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. right now im looping through the nodes and merging the values accordingly. 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. 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. dataType string
  2. Same thing with UsageMeterReadStartUsage. dataType string
  3. So after it loops through the nodes i need the UsageMeterReadStartDate to be set to the latest MAX date. dataType string
  4. same with 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. 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.

I think what you want to do is loop through all of the items first. Convert these to a DateTime object. The DateTime object lets you compare others for greater than or less than. 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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