0

I am trying to create a SQLITE query like this (first approach):

        int count;
        using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
            {
            count = ( from p in db.Table<TickRecord>()
                      where (p.TickStartDate.LocalDateTime >= start && p.TickEndtDate.LocalDateTime <= end )
                      select (int)p.DurationInSeconds ).Sum();
            }
        return count;

When running the query the application crash on the where clause.

I was able to achieve it like this (second approach):

        ObservableCollection<TickRecord> records;

        // Create a new connection
        using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
           {
           records = new ObservableCollection<TickRecord>( db.Table<TickRecord>().Select( i => i ) );
           }

        int count = records.Where( record => record.TickStartDate.LocalDateTime >= start && record.TickEndDate.LocalDateTime <= end ).Sum( record => record.DurationInSeconds );

Is there a way to achieve the same using my first approach?

Thx

2
  • 3
    What exception did you receive? Commented Apr 29, 2016 at 22:34
  • 1
    Exception thrown: 'System.NotSupportedException' in SQLite.Net.dll Member access failed to compile expression Commented Apr 29, 2016 at 22:47

2 Answers 2

1

You should not use the member access '.LocalDateTime' within your query. The Linq processor is not able to convert '.LocalDateTime' into a sqlite query, simple because there is no equivalent function in sqlite.

As a result is throws an exception as you sad:

[...] member access failed [...].

If you need the functionality of '.LocalDateTime', then you should try to get all entries of the table from your database and use the where query later on, if you have already received all data.

int count;
using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
{
    var results = db.Table<TickRecord>().ToList();
    count = ( from p in results
              where (p.TickStartDate.LocalDateTime >= start && p.TickEndtDate.LocalDateTime <= end )
              select (int)p.DurationInSeconds ).Sum();
}
return count;
Sign up to request clarification or add additional context in comments.

Comments

0

Modified the code per whymatter:

        int count;
        using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
            {
            count = ( from p in db.Table<TickRecord>()
                      where ( p.TickStartDate >= start && p.TickEndDate <= end )
                      select (int)p.DurationInSeconds ).Sum();
            }
        return count;

Thx!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.