Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Where clause with four &&

I'm trying to create an LINQ Query with 4 arguments in the Where clause. It's a Windows 8 App project and I'm using an SQLite Database. (SQLite implementation )

Here's the code snippet:

public List<FinancialListBoxExpenseItem> retrieveExpenseItems(int month, int year, bool isPaid, StaticResources.FrequencyEnum frequencyEnum)
{
    List<FinancialListBoxExpenseItem> tmpList = null;

    connection.RunInTransaction(() =>
    {
        var items = from s in connection.Table<FinancialListBoxExpenseItem>()
                    where (s.expenseDateNextPayment.Month == month)
                       && (s.expenseDateNextPayment.Year == year)
                       && (s.expensePaidForCurrentPeriod == isPaid)
                       && (s.expenseFrequencyTypeEnum == frequencyEnum)
                    select s;
        tmpList = items.ToList<FinancialListBoxExpenseItem>();
    });

    return tmpList;
}

It throws a NotSupportedAction: Member access failed to compile expression Exception

I have no idea what does this mean and how i'm supposed to fix it.

Edit: it works without the where clause therefore the error must be related to this where clause part of the code

like image 588
Joel Avatar asked Feb 18 '23 09:02

Joel


2 Answers

Probably .Month is not supported by your LINQ provider. You'll have to work around that, possibly by creating specialized columns for the month and the year.

like image 140
usr Avatar answered Feb 21 '23 01:02

usr


This is how i solved the problem:

public List<FinancialListBoxExpenseItem> retrieveExpenseItems(int month, int year, bool isPaid, StaticResources.FrequencyEnum frequencyEnum)
{
    List<FinancialListBoxExpenseItem> tmpList = new List<FinancialListBoxExpenseItem>();

    connection.RunInTransaction(() =>
    {
        var items = from s in connection.Table<FinancialListBoxExpenseItem>()
                    let convertedDate = (DateTime)s.expenseDateNextPayment
                    where (convertedDate.Month == month)
                       && (convertedDate.Year == year)
                       && (s.expensePaidForCurrentPeriod == isPaid)
                       && (s.expenseFrequencyTypeEnum == frequencyEnum)
                    select s;
        tmpList = items.ToList();
    });

    return tmpList;
}
like image 39
Joel Avatar answered Feb 21 '23 01:02

Joel