Azure Cosmos DB: How to query table data by using the Table API
This article covers the following tasks:
- Querying data with the Table API
The queries in this article use the following sample
See Querying Tables and Entities for details on how to query by using the Table API.
For these queries to work, you must have an Azure Cosmos DB account and have entity data in the container. Don't have any of those? Complete the five-minute quickstart or the developer tutorial to create an account and populate your database.
Query on PartitionKey and RowKey
Because the PartitionKey and RowKey properties form an entity's primary key, you can use the following special syntax to identify the entity:
Alternatively, you can specify these properties as part of the
$filter option, as shown in the following section. Note that the key property names and constant values are case-sensitive. Both the PartitionKey and RowKey properties are of type String.
Query by using an OData filter
When you're constructing a filter string, keep these rules in mind:
- Use the logical operators defined by the OData Protocol Specification to compare a property to a value. Note that you can't compare a property to a dynamic value. One side of the expression must be a constant.
- The property name, operator, and constant value must be separated by URL-encoded spaces. A space is URL-encoded as
- All parts of the filter string are case-sensitive.
- The constant value must be of the same data type as the property in order for the filter to return valid results. For more information about supported property types, see Understanding the Table Service Data Model.
Here's an example query that shows how to filter by the PartitionKey and Email properties by using an OData
For more information on how to construct filter expressions for various data types, see Querying Tables and Entities.
Query by using LINQ
You can also query by using LINQ, which translates to the corresponding OData query expressions. Here's an example of how to build queries by using the .NET SDK:
CloudTableClient tableClient = account.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("people"); TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>() .Where( TableQuery.CombineFilters( TableQuery.GenerateFilterCondition(PartitionKey, QueryComparisons.Equal, "Smith"), TableOperators.And, TableQuery.GenerateFilterCondition(Email, QueryComparisons.Equal,"Ben@contoso.com") )); await table.ExecuteQuerySegmentedAsync<CustomerEntity>(query, null);
In this tutorial, you've done the following:
- Learned how to query by using the Table API
You can now proceed to the next tutorial to learn how to distribute your data globally.