0

I'm trying to retrieve SQL records where the rowversion is greater than a certain value. In SQL this is trivial (WHERE RowVersion > x), however not so in Dynamic Linq queries.

In my EF model I have applied the Timestamp annotation to the appropriate column.

[Timestamp]
public byte[] RowVersion { get; set; }

And used a static Compare Extension for the byte[] comparison (source: (https://stackoverflow.com/a/42502969/3424480)

static class MiscExtension
{
    public static int Compare(this byte[] b1, byte[] b2)
    {
        if (b1 == null && b2 == null)
            return 0;
        else if (b1 == null)
            return -1;
        else if (b2 == null)
            return 1;
        return ((IStructuralComparable)b1).CompareTo(b2, Comparer<byte>.Default);
    }
}

I've tried the below style Dynamic Linq expression with no luck (maxRV is a little endian Byte[]).

.Where("RowVersion.Compare(@0) > 0", maxRV);

This throws "No applicable aggregate method 'Compare' exists" which after some researching I believe this is because 'Compare' is not a valid enumerable method. Any pointers on where I might have gone wrong / alternative approaches appreciated.

user3424480
  • 362
  • 2
  • 6
  • 19
  • 1
    And what SQL are you expecting that code to turn into? How are you expecting the query provider to know how to turn what you have into that? – Servy Jan 25 '19 at 20:53
  • @Servy updated post, forgot to complete the expression (> 0) – user3424480 Jan 25 '19 at 20:57
  • 1
    That does not answer either of the questions I asked. – Servy Jan 25 '19 at 20:59
  • As @Servy pointed out, EF doesn't know how to convert what you provided it with to the intendent SQL. Why don't you simply provide it with the exact SQL condition `.Where("RowVersion > @0", maxRV);`? – Racil Hilan Jan 25 '19 at 21:25
  • @RacilHilan already tried that, Operator '>' incompatible with operand types 'Byte[]' and 'Byte[]' – user3424480 Jan 25 '19 at 21:30

0 Answers0