/// <summary>
 /// Executes the queries.
 /// </summary>
 /// <param name="batch">The batch.</param>
 public void ExecuteQuery(PrescriptionsQueryBatch batch)
 {
     ExecuteQuery(batch.Queries);
 }
        /// <summary>
        /// Executes all queries.
        /// </summary>
        /// <param name="averageActCostBnfCode">The AverageActCostBnfCode BNF code.</param>
        /// <param name="averageActCostPerRegionBnfCode">The AverageActCostPerRegionBnfCode BNF code.</param>
        /// <param name="fractionActCostOfNicByRegionBnfCode">The FractionActCostOfNicByRegionBnfCode BNF code.</param>
        /// <returns></returns>
        public PrescriptionsQueryBatchResult ExecuteAllQueries(
            string averageActCostBnfCode,
            string averageActCostPerRegionBnfCode,
            string fractionActCostOfNicByRegionBnfCode)
        {
            // Create a query batch
            var batch = new PrescriptionsQueryBatch();

            // Add all the required queries to this one batch
            // For GetAverageActCost()
            batch.TryAdd(queryFactory.CalcAvgCostByCode(averageActCostBnfCode));
            // For GetTotalSpendPerPostcode()
            batch.TryAdd(queryFactory.CalcTotalSpendPerPostcode(practices));
            GetCalcAvgCostByCodeByRegionQueriesForAllRegions(averageActCostPerRegionBnfCode).ForEach(q => batch.TryAdd(q));
            // For GetAverageActCostPerRegion()
            batch.TryAdd(queryFactory.CalcAvgCostByCode(averageActCostPerRegionBnfCode));
            // For GetFractionActCostOfNicByRegion()
            PopulateQueryBatchForActCostNicFractionByRegion(batch, fractionActCostOfNicByRegionBnfCode);

            // Execute the queries using the reader
            prescriptionsReader.ExecuteQuery(batch);

            // Create container to return results
            var results = new PrescriptionsQueryBatchResult();

            // Add results to the return object
            results.GetAverageActCostResults.Add(averageActCostBnfCode, batch.GetCalcAvgCostByCode(averageActCostBnfCode).Result);
            results.GetTotalSpendPerPostcodeResults = batch.GetCalcTotalSpendPerPostcode().Result;
            results.GetAverageActCostResults.Add(averageActCostPerRegionBnfCode, batch.GetCalcAvgCostByCode(averageActCostPerRegionBnfCode).Result);
            Region.All.ForEach(r =>
                results.GetAverageActCostPerRegionResults.Add(
                    r,
                    batch.GetCalcAvgCostByCodeByRegion(r, averageActCostPerRegionBnfCode).Result));
            Region.All.ForEach(r =>
                results.GetFractionActCostOfNicByRegionResults.Add(
                    r,
                    batch.GetCalcTotalActCostByCodeByRegion(r, fractionActCostOfNicByRegionBnfCode).Result
                    / batch.GetCalcTotalNicByCodeByRegion(r, fractionActCostOfNicByRegionBnfCode).Result));

            // Return 
            return results;
        }
        /// <summary>
        /// Populates the query batch for GetFractionActCostOfNicByRegion.
        /// </summary>
        /// <param name="queryBatch">The query batch.</param>
        /// <param name="bnfCode">The BNF code.</param>
        private void PopulateQueryBatchForActCostNicFractionByRegion(PrescriptionsQueryBatch queryBatch, string bnfCode)
        {
            // Query the average Actual Cost for each region
            Region.All.ForEach(
                r => queryBatch.TryAdd(queryFactory.CalcTotalActCostByCodeByRegion(bnfCode, r, practices)));

            // Query the average NIC for each region
            Region.All.ForEach(r => queryBatch.TryAdd(queryFactory.CalcTotalNicByCodeByRegion(bnfCode, r, practices)));
        }
        /// <summary>
        /// Gets the average Actual Cost as a decimal fraction of the average NIC for each region.
        /// </summary>
        /// <param name="bnfCode">The BNF code.</param>
        /// <returns>A fraction for each region.</returns>
        public Dictionary<Region, decimal> GetFractionActCostOfNicByRegion(string bnfCode)
        {
            // Get region count
            int regionCount = Region.All.Count;

            // Create new batch of queries to send to reader
            var queryBatch = new PrescriptionsQueryBatch();

            // Get the queries
            PopulateQueryBatchForActCostNicFractionByRegion(queryBatch, bnfCode);

            // Execute the queries using the reader
            prescriptionsReader.ExecuteQuery(queryBatch);

            // Calculate return dictionary
            var result = new Dictionary<Region, decimal>(regionCount);
            Region.All.ForEach(
                r =>
                result.Add(
                    r,
                    queryBatch.GetCalcTotalActCostByCodeByRegion(r, bnfCode).Result
                    / queryBatch.GetCalcTotalNicByCodeByRegion(r, bnfCode).Result));

            return result;
        }