Solution for Cosmos : Get all items from a partition in a container in c# / .Net #CosmosClient
is Given Below:
I created a container to store user information for customers. The partitionKey is customerId. I want to read all items in a logical partition with customerId = X, that should return all user records with customerId = X. Is such an API available in CosmosClient in .NET SDK?
Example :
class User
{
string customerId,
string userId,
string userName
}
Assuming you are using SQL API, you can use Azure Cosmos DB SDK to fetch all the Users for your partition key. Here is the sample code:
var client = new CosmosClient("COSMOS_CONNECTION_STRING");
var container = cosmosClient.GetContainer("DATABASE_NAME", "CONTAINER_NAME");
var queryDefinition = new QueryDefinition("SELECT * FROM c")
var iterator = container.GetItemQueryIterator<User>(queryDefintion,
requestOptions: new QueryRequestOptions()
{
PartitionKey = new PartitionKey("CUSTOMER_ID")
});
var results = new List<User>();
while (iterator.HasMoreResults)
{
var result = await iterator.ReadNextAsync();
results.AddRange(result.Resource);
}
return results;