/// <summary> /// Builds the metric query. /// </summary> /// <param name="metricSource">The metric source.</param> /// <param name="dateRange">The date range.</param> /// <returns></returns> protected List <MetricValue> GetMetricValues(List <int> metricSource, DateRange dateRange, string metricKey) { var rockContext = new RockContext(); var metricService = new MetricService(rockContext); var metricQueryable = new MetricService(rockContext).Queryable(); if (!string.IsNullOrEmpty(metricKey)) { metricQueryable = metricService.Queryable().Where(a => a.Title.EndsWith(metricKey)); } else { metricQueryable = metricService.GetByIds(metricSource); } var metricValueQueryable = metricQueryable.SelectMany(a => a.MetricValues).AsQueryable().AsNoTracking(); // filter by date context if (dateRange != null) { metricValueQueryable = metricValueQueryable.Where(a => a.MetricValueDateTime >= dateRange.Start && a.MetricValueDateTime <= dateRange.End); } // filter by campus context if (CampusContext != null) { metricValueQueryable = metricValueQueryable.Where(a => a.EntityId == CampusContext.Id); } // filter by schedule context if (ScheduleContext != null) { var scheduleTime = new ScheduleService(rockContext).Get(ScheduleContext.Guid).StartTimeOfDay; metricValueQueryable = metricValueQueryable.Where(a => scheduleTime == DbFunctions.CreateTime( a.MetricValueDateTime.Value.Hour, a.MetricValueDateTime.Value.Minute, a.MetricValueDateTime.Value.Second ) ); } // filter by group context if (GroupContext != null) { metricValueQueryable = metricValueQueryable.Where(a => a.ForeignId == GroupContext.Id); } return(metricValueQueryable.ToList()); }