StartsWith Filter on Azure Table Storage columns in C# & JavaScript

Azure Table Storage is quite limited in the ways you can add filters to your queries. E.g. asking for all rows where PartitionKey starts with ‘abc’ is one of those cases that is not supported directly, but can be achieved with a little trick.

Background

Azure Table Storage allows for some basic ODATA filters to be passed with your query. You can for example filter columns with equals, lower than or greater than. While it’s intuitive how this might work for numbers, this also works with strings using the Lexicographic order.

For example:

  • aaa < aab
  • abb > aba
  • abc > ab
  • abc < ac

With this in mind we can compose filters to ask for all strings that are greater or equal (ge) to our prefix (e.g. abc) but lower than (lt) an upper limit (abd).

As an ODATA filter here’s what it would look like:
PartitionKey ge 'abc' and PartitionKey lt 'abd'

To easily generate the correct filter I’d recommend using a small helper function.

JavaScript

C#

This C# helper function can also be found in my Azure.Data.Tables Extensions NuGet package.

Note

While this does work quite well, depending on your data structure this query might result in a full table scan which can greatly impact your code’s performance!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.