Search code examples
c#azure-table-storageazure-tablequery

query rowkey with greater than in azure table storage


I used this link and the quoted white paper to allow me to sort data inserted into table storage. The 'entities' stored have this simplified 'schema':

public class Bla : TableEntity
{
    public Bla(){}

    public Bla(string partitionKey)
    {
        PartitionKey = partitionKey;

        // rowkey + partition = guid/pk
        // used to order blas
        RowKey = (DateTime.UtcNow.Ticks - DateTime.MinValue.Ticks).ToString();
    }
}

I can easily get a 'page' (maximum page size 1000) sorted by the rowkey ascendingly like so:

var query =
(from s in _table.CreateQuery<Bla>()
where
s.PartitionKey == _partitionKey &&
string.Compare(s.RowKey, rowKeyToUse, StringComparison.Ordinal) > 0 
select s).AsTableQuery();

I have this use case where I would like to select any entity where the rowkey is greater than a long (the rowkey is just ticks - a long expressed as string). So I tried this:

var query =
(from s in _table.CreateQuery<Bla>()
where
s.PartitionKey == _partitionKey &&
s.RowKey.CompareTo(635919954373048408) > 0 &&
string.Compare(s.RowKey, rowKeyToUse, StringComparison.Ordinal) > 0 
select s).AsTableQuery();

but I get a 404. Any ideas?


Solution

  • I think the issue with your query was that you're comparing different types with each other. Namely the string rowkey with your long timestamp.

    The linq query which should work is:

    from entry in table 
    where entry.RowKey.CompareTo("635919954373048408") >= 0 
    select entry