Question

I have a Windows 8 store application and I'm using the latest version on SQLite for my database.

So I want to return some records from the database and I want to order them by more that one column. However SQLite doesn't seem to have the ThenBy statement? So my LINQ statement is below:

from i in connection.Table<MyTable>()
where i.Type == type 
orderby i.Usage_Counter
// ThenBy i.ID
select i);

So how do I sort by multiple columns in SQLite without doing another LINQ statement?

Was it helpful?

Solution

You can't use SQL-Net's LINQ implementation to sort by multiple columns, since it doesn't have ThenBy implemented.

You can work around this by using the Query method on the connection:

var sorted =
    connection.Query<MyTable>(
    "select * from MyTable where Type = ? order by Usage_Counter, ID",
    // you may have to cast to int here...
    type);

Another option is to use your result from above and call ToList() on it. You can then sort the results by multiple values:

var tables =
from i in connection.Table<MyTable>()
where i.Type == type 
select i;

var tmp = tables.ToList();

var sorted =
from i in tmp
orderby i.Usage_Counter, i.ID
select i;

OTHER TIPS

When using query syntax, you simply supply a comma-delimited list of fields to sort by:

from i in connection.Table<MyTable>()
where i.Type == type 
orderby i.Usage_Counter, i.ID
select i

With method syntax, you can use ThenBy:

connection.Table<MyTable>()
          .Where(i => i.Type == type)
          .OrderBy(i => i.Usage_Counter)
          .ThenBy(i => i.ID)

Edit - Sorry to be the downer here, but it appears you can't use Entity Framework with Windows 8 Store Apps: Is EntityFramework available for Windows 8 Store Apps?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top