Question

I'm using a SQLite-Net database and would like to order by a comparison, like this:

var value1 = 10;

var items = connection.Table<Item>.OrderBy(i => i.Field1 > value1).ToArray();

(Note: value1 and Field1 are both integers)

This throws an exception:

System.NotSupportedException: Order By does not support: i => i.Field1 > value1

As a workaround, I've now split this query into two parts:

  1. Select the item where Field1 is larger than value1
  2. Select the item where Field1 is smaller than or equal to value1

And then these results are combined to a single result.

Is there a better way to handle this using SQLite-Net?

Was it helpful?

Solution

My first approach was:

var value1 = 10;
var items = new List<Item>();
items.AddRange(connection.Table<Item>.Where(i => i.Field1 <= value1));
items.AddRange(connection.Table<Item>.Where(i => i.Field1 > value1));

But I finally settled for a single query:

var value1 = 10;
var items = connection
              .Query<Item>(@"SELECT * 
                             FROM [Item] 
                             ORDER BY [Field1] > ? ASC", value1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top