1

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?

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • How does `s.RowKey.CompareTo(635919954373048408)` even compile when the RowKey is a string? – Thomas Jungblut Feb 25 '16 at 13:34
  • because 635919954373048408 is an object which s.RowKey.CompareTo() expects?!! It compiles but that's not the point. My rowkey is a 'long' (ticks) stored as string because rowkey is a string. The question is how can I select everything above a threshold - e.g. 635919954373048408 – cs0815 Feb 25 '16 at 13:41
  • Yeah, but the comparison will never work because the types are not the same?! – Thomas Jungblut Feb 25 '16 at 14:07
  • Ok please make a suggestion then (-: – cs0815 Feb 25 '16 at 14:07
  • obviously `s.RowKey.CompareTo("635919954373048408")`? – Thomas Jungblut Feb 25 '16 at 14:10
  • yeah sure but I would like to select anything where the rowkey is greater than a given long if that makes sense (sorry if this was not clear). Unfortunately, azure table storage querying is not as straightforward as linq is not (yet?) fully supported. – cs0815 Feb 25 '16 at 14:13
  • 2
    With linq it should be exactly the same: `from entry in table where entry.RowKey.CompareTo("635919954373048408")>=0 select entry ` – Thomas Jungblut Feb 25 '16 at 14:40
  • @ThomasJungblut has got the answer and should post it as such. :) – Emily Gerner Feb 25 '16 at 17:38

1 Answers1

3

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
Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
  • So will your solution return anything where the string, which is actually a long, is greater than 63591995437304808? – cs0815 Feb 25 '16 at 18:45
  • I think it is worth pointing out that if you're going to use a string representation of a long for your row or partition key and expect everything to sort as expected it must be 0 padded sufficiently or you can have situations where for example values 1 to 10 would be sorted as: 1,10,2,3,4,5,6,7,8,9. – Caleb Vear Oct 18 '16 at 23:53