public async Task <FetchModelCollectionResult <TestModel2> > FetchTestModel2ByDateAsync(DateTime filterDate) { var result = new FetchModelCollectionResult <TestModel2>(); var allItems = new List <TestModel2>(); //all stored dates are in utc - make sure apples == apples filterDate = filterDate.ToUniversalTime(); try { result.Notification = CheckServiceCanOperate(); if (!result.IsValid()) { return(result); } var queryable = _container.GetItemLinqQueryable <CosmosDocument <TestModel2> >(allowSynchronousQueryExecution: true, requestOptions: PartitionRequestOptions); var query = queryable.Where(cd => cd.Model.Date >= filterDate.Date && cd.Model.Date <= filterDate.AddDays(1).Date); var iterator = query.ToFeedIterator(); using (iterator) { while (iterator.HasMoreResults) { FeedResponse <CosmosDocument <TestModel2> > response = await iterator.ReadNextAsync(); allItems.AddRange(response.Select(d => d.Model)); } } result.ModelCollection = allItems; } catch { Debug.WriteLine($"Error while trying to fetch all {typeof(TestModel2).Name} items from cosmos"); result.Fail("Error"); } return(result); }
public async Task <FetchModelCollectionResult <T> > FetchModelCollectionAsync <T>() where T : ModelBase, new() { //doc: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement/Program.cs var result = new FetchModelCollectionResult <T>(); var allItems = new List <T>(); try { result.Notification = CheckServiceCanOperate(); if (!result.IsValid()) { return(result); } var queryable = _container.GetItemLinqQueryable <CosmosDocument <T> >(requestOptions: PartitionRequestOptions); var query = queryable.Where(cd => cd.Type == typeof(T).Name); var iterator = query.ToFeedIterator(); using (iterator) { while (iterator.HasMoreResults) { FeedResponse <CosmosDocument <T> > response = await iterator.ReadNextAsync(); allItems.AddRange(response.Select(d => d.Model)); } } result.ModelCollection = allItems; } catch { Debug.WriteLine($"Error while trying to fetch all {typeof(T).Name} items from cosmos"); result.Fail("Error"); } return(result); }