/// <summary>
        /// Loads the most recent Device telemetry.
        /// </summary>
        /// <param name="deviceId">
        /// The ID of the Device for which telemetry should be returned.
        /// </param>
        /// <param name="minTime">
        /// The minimum time of record of the telemetry that should be returned.
        /// </param>
        /// <returns>
        /// Telemetry for the Device specified by deviceId, inclusively since
        /// minTime.
        /// </returns>
        public async Task <IEnumerable <DeviceTelemetryModel> > LoadLatestDeviceTelemetryAsync(
            string deviceId,
            IList <DeviceTelemetryFieldModel> telemetryFields,
            DateTime minTime)
        {
            IEnumerable <DeviceTelemetryModel> result = new DeviceTelemetryModel[0];
            IEnumerable <DeviceTelemetryModel> blobModels;

            var telemetryBlobReader = await _blobStorageManager.GetReader(_telemetryDataPrefix, minTime);

            foreach (var telemetryStream in telemetryBlobReader)
            {
                try
                {
                    blobModels = LoadBlobTelemetryModels(telemetryStream.Data, telemetryFields);
                }
                catch
                {
                    continue;
                }

                if (blobModels == null)
                {
                    break;
                }

                int preFilterCount = blobModels.Count();

                blobModels =
                    blobModels.Where(
                        t =>
                        (t != null) &&
                        t.Timestamp.HasValue &&
                        t.Timestamp.Value >= minTime);

                if (preFilterCount == 0)
                {
                    break;
                }

                result = result.Concat(blobModels);
            }

            if (!string.IsNullOrEmpty(deviceId))
            {
                result = result.Where(t => t.DeviceId == deviceId);
            }

            return(result);
        }
        /// <summary>
        /// Loads the most recent Device telemetry.
        /// </summary>
        /// <param name="deviceId">
        /// The ID of the Device for which telemetry should be returned.
        /// </param>
        /// <param name="minTime">
        /// The minimum time of record of the telemetry that should be returned.
        /// </param>
        /// <returns>
        /// Telemetry for the Device specified by deviceId, inclusively since
        /// minTime.
        /// </returns>
        public async Task <IEnumerable <DeviceTelemetryModel> > LoadLatestDeviceTelemetryAsync(
            string deviceId,
            DateTime minTime)
        {
            IEnumerable <DeviceTelemetryModel> result = new DeviceTelemetryModel[0];

            CloudBlobContainer container =
                await BlobStorageHelper.BuildBlobContainerAsync(this._telemetryStoreConnectionString, _telemetryContainerName);

            IEnumerable <IListBlobItem> blobs =
                await BlobStorageHelper.LoadBlobItemsAsync(
                    async (token) =>
            {
                return(await container.ListBlobsSegmentedAsync(
                           _telemetryDataPrefix,
                           true,
                           BlobListingDetails.None,
                           null,
                           token,
                           null,
                           null));
            });

            blobs = blobs.OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t));

            CloudBlockBlob blockBlob;
            IEnumerable <DeviceTelemetryModel> blobModels;

            foreach (IListBlobItem blob in blobs)
            {
                if ((blockBlob = blob as CloudBlockBlob) == null)
                {
                    continue;
                }

                try
                {
                    blobModels = await LoadBlobTelemetryModelsAsync(blockBlob);
                }
                catch
                {
                    continue;
                }

                if (blobModels == null)
                {
                    break;
                }

                int preFilterCount = blobModels.Count();

                blobModels =
                    blobModels.Where(
                        t =>
                        (t != null) &&
                        t.Timestamp.HasValue &&
                        t.Timestamp.Value >= minTime);

                if (preFilterCount == 0)
                {
                    break;
                }

                result = result.Concat(blobModels);

                if (preFilterCount != blobModels.Count())
                {
                    break;
                }
            }

            if (!string.IsNullOrEmpty(deviceId))
            {
                result = result.Where(t => t.DeviceId == deviceId);
            }

            return(result);
        }
        /// <summary>
        /// Loads the most recent Device telemetry.
        /// </summary>
        /// <param name="deviceId">
        /// The ID of the Device for which telemetry should be returned.
        /// </param>
        /// <param name="minTime">
        /// The minimum time of record of the telemetry that should be returned.
        /// </param>
        /// <returns>
        /// Telemetry for the Device specified by deviceId, inclusively since
        /// minTime.
        /// </returns>
        public async Task <IEnumerable <DeviceTelemetryModel> > LoadLatestDeviceTelemetryAsync(
            string deviceId,
            IList <DeviceTelemetryFieldModel> telemetryFields,
            DateTime minTime)
        {
            IEnumerable <DeviceTelemetryModel> result = new DeviceTelemetryModel[0];

            CloudBlobContainer container =
                await BlobStorageHelper.BuildBlobContainerAsync(this._telemetryStoreConnectionString, this._telemetryContainerName);

            IEnumerable <IListBlobItem> blobs =
                await BlobStorageHelper.LoadBlobItemsAsync(
                    async (token) =>
            {
                return(await container.ListBlobsSegmentedAsync(
                           this._telemetryDataPrefix,
                           true,
                           BlobListingDetails.None,
                           null,
                           token,
                           null,
                           null));
            });

            blobs = blobs
                    .OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t));

            CloudBlockBlob blockBlob;
            IEnumerable <DeviceTelemetryModel> blobModels;

            foreach (IListBlobItem blob in blobs)
            {
                if ((blockBlob = blob as CloudBlockBlob) == null)
                {
                    continue;
                }

                // Translate LastModified to local time zone.  DateTimeOffsets
                // don't do this automatically.  This is for equivalent behavior
                // with parsed DateTimes.
                if ((blockBlob.Properties != null) &&
                    blockBlob.Properties.LastModified.HasValue &&
                    (blockBlob.Properties.LastModified.Value.LocalDateTime < minTime))
                {
                    break;
                }

                try
                {
                    blobModels = await LoadBlobTelemetryModelsAsync(blockBlob, telemetryFields);
                }
                catch
                {
                    continue;
                }

                if (blobModels == null)
                {
                    break;
                }

                int preFilterCount = blobModels.Count();

                blobModels =
                    blobModels.Where(
                        t =>
                        (t != null) &&
                        t.Timestamp.HasValue &&
                        t.Timestamp.Value >= minTime);

                if (preFilterCount == 0)
                {
                    break;
                }

                result = result.Concat(blobModels);
            }

            if (!string.IsNullOrEmpty(deviceId))
            {
                result = result.Where(t => t.DeviceId == deviceId);
            }

            return(result);
        }