Firstly, as a general overview of the problem, I am writing an MVC web application in that needs to query my repository based upon several different business logic rules. The user will first enter text into the webpage, and said text will be parsed in my business layer and the appropriate where clause query will be sent to the repository.
So, for an example of my code:
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);
}
When I run this, I get an run-time exception ” Cannot invoke a non-delegate type.” What I believe is happening (and of course correct me if I’m wrong about this), is that the actual expression is enumerated in the repository, at which point filterVals is no longer in scope. So… is there a way to get these values into my where clause expressions? Any help would be greatly appreciated.
Try changing
to
and see what happens.