'The parameter 'f' was not bound in the specified LINQ to Entities query expression'

3.5k views Asked by At

Using EF 6. Can someone tell me what am I doing wrong?

public List<SearchResult> SearchDocuments(List<SearchCriteria> searchCriterias)
{
    List<SearchResult> results = new List<SearchResult>();
    var fieldSettings = LoadCoordinateSystemFieldSettings(searchCriterias);
    using (var context = CreateContext())
    {
        var tractsQuery = PredicateBuilder.False<v_UploadByTract_ActiveUploads>();
        foreach (var searchCriteria in searchCriterias)
        {
            var queryBuilder = PredicateBuilder.True<v_UploadByTract_ActiveUploads>();

            // tractsQuery
            if (searchCriteria.StateAPI.HasValue)
                queryBuilder = queryBuilder.And(a => a.StateAPI == searchCriteria.StateAPI.Value);
            if (searchCriteria.CountyAPI.HasValue)
                queryBuilder = queryBuilder.And(a => a.CountyAPI == searchCriteria.CountyAPI.Value);

            // ...
            // ... many more similar IF-ANDs
            // ...

            tractsQuery = tractsQuery.Or(queryBuilder);
        }

        var searchQuery = context.v_UploadByTract_ActiveUploads.AsExpandable().Where(tractsQuery).ToList();
        //.Select(a => SearchResult.Create(a));
        //results.AddRange(searchQuery.ToList());
    }

    return results;
}

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                         Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
    }
}
2

There are 2 answers

0
Deepak On BEST ANSWER

quetzalcoatl thanks for the answer. The other post did it. From else where I had come across AsExpandable() and overlooked this one thinking it's talking about the same. After reading your post and re-reading the discussion, this worked. Since your post shows up as "comment", I am unable to mark that as the answer. Any suggestions on how to consolidate those nasty IFs.

C# PredicateBuilder Entities: The parameter 'f' was not bound in the specified LINQ to Entities query expression

2
user326608 On

For anyone else googling into here, my solution was to Visit a modified expression before returning it into the expression tree. This bound the lambda parameter.