static void Main() { var inputRateDataRaw = File.ReadAllLines(@"c:\temp\RATES_IN.csv"); DateTime startDate = new DateTime(1997, 05, 31); // parse the input dates and rates var rateDataFiltered = inputRateDataRaw .Select(rateData => { var dataComponents = rateData.Split(','); DateTime rateDate = DateTime.ParseExact(dataComponents[0], DATE_FORMAT_IN, null); decimal rate = decimal.Parse(dataComponents[1]); return(new RateEntry() { StartDate = rateDate, Rate = rate }); }) .Where(a => a.StartDate > startDate) .OrderBy(a => a.StartDate) .ToList(); List <RateEntry> rateRanges = new List <RateEntry>(); for (int i = 0; i < rateDataFiltered.Count; i++) { RateEntry next = ((i + 1) == rateDataFiltered.Count) ? null : rateDataFiltered[i + 1]; RateEntry last = (i == 0) ? null : rateDataFiltered[i - 1]; RateEntry now = rateDataFiltered[i]; // if this is the first rate, or if the last rate isn't this rate, this is a new entry. if (last == null || last.Rate != now.Rate) { rateRanges.Add(now); } // if the next rate isn't this one, then the current entry expiration is this one's start date. if (next == null || next.Rate != now.Rate) { rateRanges.Last().ExpirationDate = now.StartDate; } } // write the data out using (StreamWriter writer = new StreamWriter(@"c:\temp\RATES_OUT.csv")) { writer.WriteLine("ROWID;STARTDATE;EXPIRATIONDATE;RATE"); for (int i = 0; i < rateRanges.Count; i++) { RateEntry rateEntry = rateRanges[i]; writer.WriteLine($"{i};{rateEntry.StartDate.ToString(DATE_FORMAT_OUT)};{rateEntry.ExpirationDate.ToString(DATE_FORMAT_OUT)};{rateEntry.Rate}"); } } Console.ReadKey(); }
static void Main() { var fedFundDataRaw = File.ReadAllLines(@"c:\temp\FEDFUNDS_IN.csv"); string dateFormatIn = "yyyy-MM-dd"; string dateFormatOut = "yyyy-MM-dd"; DateTime startDate = new DateTime(1997, 05, 31); // parse the input dates and rates var fedFundData = fedFundDataRaw.Select(rateData => { var dataComponents = rateData.Split(','); DateTime rateDate = DateTime.ParseExact(dataComponents[0], dateFormatIn, null); decimal rate = decimal.Parse(dataComponents[1]); return(new RateEntry() { Date = rateDate, Rate = rate }); }); decimal?lastSeenRate = null; var selectedRateData = fedFundData .Where(a => a.Date > startDate) // skip anything that is older than our cut-off date .OrderBy(a => a.Date) // order by date .Where((rateEntry) => { if (lastSeenRate != null && rateEntry.Rate == lastSeenRate.Value) { return(false); // we're not interested in this entry if this date has the same as the one we've just seen } lastSeenRate = rateEntry.Rate; // set this rate as the last rate we've seen return(true); }) .ToList(); // write the data out using (StreamWriter writer = new StreamWriter(@"c:\temp\FEDFUNDS_OUT.csv")) { writer.WriteLine("ROWID;STARTDATE;EXPIRATIONDATE;RATE"); for (int i = 0; i < selectedRateData.Count; i++) { RateEntry rateEntry = selectedRateData[i]; RateEntry nextEntry = ((i + 1) == selectedRateData.Count) ? null : selectedRateData[i + 1]; writer.WriteLine($"{i};{rateEntry.Date.ToString(dateFormatOut)};{nextEntry?.Date.ToString(dateFormatOut)};{rateEntry.Rate}"); } } Console.ReadKey(); }