CRUD Operations with Azure Table Storage in an Azure Function – R

In an earlier post, we discussed how to insert a new item in the Azure Storage Table. In this article, we will delve into how to retrieve data from Azure Storage Table.

Retrieve a Single Entity

In the previous article, we had partitioned the entities based on their first letter of their Title. Here is how our table looked at end our insert operations.

As mentioned before, we would use CloudTable to retrieve the entity we desire. For the sake of example, let us assume that the RowKey of the desired record would be passed via QueryString in the Http Request.

[FunctionName("TodoGetOne")]
public static async Task<IActionResult> GetOne(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[Table("todos")] CloudTable todoTable,
ILogger log)
{
    string id = req.Query["id"];

    var tableQuery = new TableQuery<TodoTableEntity>();

    tableQuery.FilterString = TableQuery.CombineFilters(
        TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.NotEqual, "Key"), 
        TableOperators.And, 
        TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.Equal, id));

    var result = todoTable.ExecuteQuery(tableQuery);
    return new OkObjectResult(result);
}

As you can observe, we have used the TableQuery to filter our desired entity.

var tableQuery = new TableQuery<TodoTableEntity>();

tableQuery.FilterString = TableQuery.CombineFilters(
    TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.NotEqual, "Key"), 
    TableOperators.And, 
    TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.Equal, id));

The TableQuery.FilterString property enables us to provide Custom filters for the query. This is further facilitated by supporting utility methods in TableQuery, such as CombineFilters, and GeneratedFilterCondition. In our case, as seen in code above, we are filtering the data where the PartitionKey is not equal to text Key (remember, the partition key in our example is a special partition which keeps track of next Primary Id available ) and RowKey equavalent to the Id passed via querystring.

Further, we execute the query using the CloudTable.ExecuteQuery method.

var result = todoTable.ExecuteQuery(tableQuery);

That’s all you would need to fetch the data from Azure Table Storage.

Retrieve One Entity Using Binding

Table Binding also helps us retrieve a single record skipping a lot of boiler plate code, provided we know the parition key and the row key. For example, consider the following code.

[FunctionName("TodoGetOneBinding")]
public static async Task<IActionResult> TodoGetOneBinding1(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "TodoGetOneBinding/{partition}/{id}")] HttpRequest req,
[Table("todos", "{partition}", "{id}")] TodoTableEntity todo,
ILogger log)
{
    return new OkObjectResult(todo);
}

This effectively does the same as the code in our previous example, but provides a less cluttered code. The {parition} and {id} parameters from the Route is used as parameters for filter the table here.

Retrieve Multiple Entities

I guess there is very little to explain here once we have done the above examples. So let us write the code straightaway for retrieve all the todo items

[FunctionName("TodoGetAll")]
public static async Task<IActionResult> GetAll(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    [Table("todos")] CloudTable todoTable,
    ILogger log)
{
    var tableQuery = new TableQuery<TodoTableEntity>();
    tableQuery.SelectColumns = new List<string> { nameof(TodoTableEntity.Description) };
    tableQuery.FilterString = TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.NotEqual, "Key");

    var result = todoTable.ExecuteQuery(tableQuery);
    return new OkObjectResult(result);
}

That’s all for now. We will continue our journey exploring the Azure Cloud Storage in upcoming articles.

2 thoughts on “CRUD Operations with Azure Table Storage in an Azure Function – R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s