1

首先,作为问题的一般概述,我正在编写一个 MVC Web 应用程序,它需要根据几个不同的业务逻辑规则查询我的存储库。用户将首先在网页中输入文本,该文本将在我的业务层中进行解析,并将适当的 where 子句查询发送到存储库。

因此,以我的代码为例:

public Document GetDocuments(string txtFilter)
{
    string [] filterVals = // do some business logic here to parse txtFilter into the filterValsArray

    // filterQuery will utlimately contain the where clause for the repository
    Expression<Func<DocumentListViewModel, bool>> filterQuery = null;

    // case 1, when filterVals has only 1 value:
    if (filterVals[0].Length == 1)
    {
        filterQuery = x => x.SomeField == filterVals[0];
    }
    else // assign filterQuery to another set of expressions

    // repository call
    return _unitOfWork.DocumentRepository.Get(filterQuery);
}

当我运行它时,我得到一个运行时异常“无法调用非委托类型”。我相信正在发生的事情(如果我错了,当然纠正我),是在存储库中枚举了实际的表达式,此时 filterVals 不再在范围内。那么...有没有办法将这些值放入我的 where 子句表达式中?任何帮助将不胜感激。

4

2 回答 2

1

尝试改变

    Expression<Func<DocumentListViewModel, bool>> filterQuery = null;

    Func<DocumentListViewModel, bool> filterQuery = null;

看看会发生什么。

于 2013-01-03T17:14:58.220 回答
0

我假设您正在编译文档存储库中的表达式?如果不是,您需要在参数上调用 Compile()

于 2013-01-03T17:11:22.990 回答