Search code examples
linqsqlitec#-4.0windows-store-appssqlite-net

LINQ in SQLite for Windows store app does not have 'ThenBy' to order by multiple columns


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?


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;