4

我正在尝试在 Where 子句中创建一个带有 4 个参数的 LINQ 查询。这是一个 Windows 8 App 项目,我使用的是 SQLite 数据库。(SQLite 实现

这是代码片段:

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;
}

它抛出 NotSupportedAction: Member access failed to compile expression 异常

我不知道这是什么意思以及我应该如何解决它。

编辑:它在没有 where 子句的情况下工作,因此错误必须与代码的这个 where 子句部分有关

4

3 回答 3

5

您的 LINQ 提供程序可能.Month不支持。您必须解决这个问题,可能通过为月份和年份创建专门的列。

于 2012-11-24T20:39:32.913 回答
3

这就是我解决问题的方法:

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;
}
于 2012-11-24T22:18:50.763 回答
0

在我的应用程序中,我NotSupportedException在运行 LINQ 查询时得到了一个,并且显示了异常的详细信息

成员访问编译表达式失败

正如这个线程让我知道的那样,发出的问题似乎是由DateTime查询中引用的变量引起的。除了找到像这样的 StackOverflow 线程来为您指明正确的方向之外,我不确定任何人应该如何自己解决这个问题,但对我来说,改变我编写查询的方式解决了这个问题。

此语法抛出“NotSupportedException”:*

IEnumerable<Foo> foos = connection.Table<Foo>().Where(foo => foo.Timestamp.Year == year);

切换到这种语法效果很好:

IEnumerable<Foo> foos = connection.Table<Foo>().Where(
    delegate(Foo foo)
{
    return (foo.Timestamp.Year == year);
});
于 2017-03-10T05:51:50.127 回答