问题
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
回答1:
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.
回答2:
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;
}
回答3:
In my App I was getting a NotSupportedException
when running a LINQ query, and the details of the exception showed
Member access failed to compile expression
As this thread let me know, the issued seemed to be caused by a DateTime
variable that was being referenced in the query. Outside of finding a StackOverflow thread like this one to point you in the right direction, I'm not sure how anyone is supposed to figure that out on their own, but for me, changing the way I was writing the query fixed the problem.
This syntax was throwing the "NotSupportedException":*
IEnumerable<Foo> foos = connection.Table<Foo>().Where(foo => foo.Timestamp.Year == year);
Switching to this syntax worked just fine:
IEnumerable<Foo> foos = connection.Table<Foo>().Where(
delegate(Foo foo)
{
return (foo.Timestamp.Year == year);
});
来源:https://stackoverflow.com/questions/13542558/linq-where-clause-with-four