public List <IVintage> MakeDense(List <IVintage> vintages) { foreach (Vintage v in vintages.Where(x => x.Observations != null)) { foreach (Observation o in v.Observations) { o.Vintage = v; } } IList <IObservation> denseObservations = MakeDense(vintages.SelectMany(x => x.Observations).ToList()); List <IVintage> denseVintages = new List <IVintage>(); foreach (var grp in denseObservations.GroupBy(x => new { x.Vintage.VintageDate, x.Vintage.Symbol })) { IVintage v = grp.First().Vintage; v.Observations = grp.ToList(); denseVintages.Add(v); } foreach (IVintage emptyVintage in vintages.Where(x => !(x.Observations?.Any() ?? false)).OrderBy(x => x.VintageDate)) { IVintage copyFrom = denseVintages.OrderByDescending(x => x.VintageDate).FirstOrDefault(x => x.Symbol == emptyVintage.Symbol && x.VintageDate < emptyVintage.VintageDate); if (copyFrom != null) { emptyVintage.Observations = copyFrom.Observations; } denseVintages.Add(emptyVintage); } return(denseVintages); }
private void CopyDictToDenseObs(IVintage vintage, Dictionary <DateTime, IObservation> dict, List <IObservation> denseObs) { foreach (IObservation d in dict.Values) { IObservation newObs; // obs is new if vintage date is != vintage.VintageDate if (d.VintageDate != vintage.VintageDate) { newObs = new Observation { Symbol = d.Symbol, VintageDate = vintage.VintageDate, ObsDate = d.ObsDate, Value = d.Value } } ; else { newObs = d; } denseObs.Add(newObs); } }