public IEnumerable <MetricGraph> GraphData(int companyId, Guid?reportUniqueId = null) { var metricGraphs = new List <MetricGraph>(); // Get the graph data for multiple metrics var graphData = _metricRepository.GraphData(companyId, reportUniqueId); // loop through each unique metric in graph data and create a MetricGraph for each var graphPoints = graphData as IList <GraphPoint> ?? graphData.ToList(); foreach (var metricGuid in graphPoints.Select(m => m.UniqueId).Distinct().ToList()) { var metricGraph = new MetricGraph { UniqueId = metricGuid }; // Get just the data points for this metric, ordered by date var dataPoints = graphPoints.Where(m => m.UniqueId == metricGuid).OrderBy(m => m.Date).ToList(); // We need to know which date is the current date var currentMonthDataPoint = dataPoints.FirstOrDefault(m => m.IsCurrentDate); // If there isn't a data point flagged as the current date, use the max date provided with a value var firstPoint = dataPoints.Where(m => m.Actual != 0 && m.Actual != null && m.Date <= DateTime.UtcNow).OrderByDescending(m => m.Date).FirstOrDefault(); if (firstPoint != null) { var currentDate = currentMonthDataPoint != null ? currentMonthDataPoint.Date : firstPoint.Date; // The thumbnail graph should only run to the current date metricGraph.ThumbnailGraph = dataPoints.Where(m => m.Date <= currentDate).OrderByDescending(m => m.Date).Take(12).OrderBy(m => m.Date).ToList(); } // Provide the full dataset for the full graph metricGraph.FullGraph = dataPoints; // Add this graph to our collection metricGraphs.Add(metricGraph); } // Return all of our lovely graphs return(metricGraphs); }