Byte[] comparison in Linq enumerable

228 Views Asked by At

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.

0

There are 0 best solutions below