0

We are using SQLite.NET (1.5.231) in a Xamarin project

We execute Get queries like this

var result = await db.Table<T>().Where(query).ToListAsync();

Where db is a SQLiteAsyncConnection and query is a Expression<Func<T, bool>>

If I pass something like this as query it works

(intervention.EndDate == null && intervention.StartDate == day) || 
intervention.EndDate == null && intervention.StartDate <= day && day <= DateTime.Now.Date

However I'd prefer to wrap the logic up in a function and pass it like this

public async Task<IEnumerable<Intervention>> GetAMonthsInterventionsAsync(int month, int year)
{
        var interventions = await this.dataProviderService.GetAsync<Intervention>(
                                                i => ShouldInterventionBeConsideredInMonth(month, year, i),
                                                false);
    
        return interventions;
 }

private bool ShouldInterventionBeConsideredInMonth(int month, int year, Intervention intervention)
{
    DateTime startOfMonth = new DateTime(year, month, 1).StartOfDay();
    DateTime endOfMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month)).EndOfDay();

    bool inMonth = false;

    if (intervention.StartDate <= endOfMonth && intervention.EndDate != null && intervention.EndDate >= startOfMonth)
    {
        // Intervention starts before the month ends AND ends after the month starts
        inMonth = true;
    }
    else if (intervention.StartDate <= endOfMonth && intervention.EndDate == null)
    {
        inMonth = true;
    }

    if (intervention.IsDeleted)
    {
        inMonth = false;
    }

    return inMonth;
}

That fails with a

**System.NotSupportedException:** 'Cannot compile: Parameter'

This is because SQLite cannot understand GetAMonthsInterventionsAsync. How can I use this kind of abstraction with SQlite?

2
  • That's is too complicate for SQLite to understand Commented Dec 3, 2021 at 3:38
  • I realise that so I wondering how other people encapsulate that kind of logic. I guess the only way is it to concatenate a number of SQL statements Commented Dec 4, 2021 at 16:58

0

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.