LINQ Where子句有四个&&
我正在尝试在Where子句中创建一个带有4个参数的LINQ查询。 这是一个Windows 8 App项目,我正在使用SQLite数据库。 ( SQLite实现 )
这是代码片段:
public List retrieveExpenseItems(int month, int year, bool isPaid, StaticResources.FrequencyEnum frequencyEnum) { List tmpList = null; connection.RunInTransaction(() => { var items = from s in connection.Table() where (s.expenseDateNextPayment.Month == month) && (s.expenseDateNextPayment.Year == year) && (s.expensePaidForCurrentPeriod == isPaid) && (s.expenseFrequencyTypeEnum == frequencyEnum) select s; tmpList = items.ToList(); }); return tmpList; }
它抛出一个NotSupportedAction:成员访问无法编译表达式Exception
我不知道这是什么意思,我应该怎么解决它。
编辑: 它没有where子句,因此错误必须与此where子句的代码部分相关
可能。您的LINQ提供程序不支持.Month
。 您可能需要解决这个问题,可能需要为月份和年份创建专门的列。
这就是我解决问题的方法:
public List retrieveExpenseItems(int month, int year, bool isPaid, StaticResources.FrequencyEnum frequencyEnum) { List tmpList = new List (); connection.RunInTransaction(() => { var items = from s in connection.Table () 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; }
在我的应用程序中,我在运行LINQ查询时收到NotSupportedException
,并显示exception的详细信息
成员访问无法编译表达式
正如这个post告诉我的那样,发布似乎是由查询中引用的DateTime
变量引起的。 除了找到像这样的StackOverflow线程以指向正确的方向之外,我不确定任何人应该如何自己解决这个问题,但对我来说,改变我编写查询的方式解决了问题。
这种语法抛出“NotSupportedException”:*
IEnumerable foos = connection.Table ().Where(foo => foo.Timestamp.Year == year);
切换到这种语法工作得很好:
IEnumerable foos = connection.Table ().Where( delegate(Foo foo) { return (foo.Timestamp.Year == year); });