0

I have a generic method that filters a list of entities, filtering is done by generating lambda expressions:

protected object initFilters<TEntity>(string targetEntity, List<SearchItem> searchItems, int page_size = 20, int offset = 0 , ExpressionSortCriteria<TEntity, string> SortCriteria)
{
   var SearchQuery = new SearchQuery<TEntity>();

   if (searchItems != null)
   {
      var predicat = ExpressionBuilder.GetExpression<TEntity>(searchItems).Compile();
      SearchQuery.AddFilter(predicat);
   }
   //SearchQuery.AddSortCriteria(new ExpressionSortCriteria<Profile, string> { SortExpression = (profile => profile.Libelle), Direction = SortDirection.Ascending });
   SearchQuery.Skip = offset;
   SearchQuery.Take = page_size;

   return (object)SearchQuery;
}

My SearchQuery contains criteria that will be applied to the list.

My predicate is build by a method in this way :

public static Expression<Func<T, bool>> GetExpression<T>(IList<SearchItem> filters)
{
             some code ...

             return Expression.Lambda<Func<T, bool>>(exp, param);
}

My AddFilter (in the searchQuery) is like this :

public void AddFilter(Expression<Func<TEntity, Boolean>> filter)
{
    Filters.Add(filter);
}

I have a problem with this line :

SearchQuery.AddFilter(predicat);

Error:

cannot convert from System.Func to System.Linq.Expressions.Expression>

As you see , my two methods are using

Expression<Func<T, bool>>

Any idea please how to solve this ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SirZoro
  • 151
  • 1
  • 2
  • 10
  • No, I made it like this : var SearchQuery = new SearchQuery(); if (searchItems != null) { var predicat = ExpressionBuilder.GetExpression(searchItems).Compile(); SearchQuery.AddFilter(predicat); } – SirZoro May 16 '13 at 20:25

1 Answers1

0

in your comment, you say you make

var SearchQuery = new SearchQuery<TEntity>(); 
if (searchItems != null) { 
  var predicat = ExpressionBuilder.GetExpression<TEntity>(searchItems).Compile();          
  SearchQuery.AddFilter(predicat); 
}

of course, if your compile your Expression, it's no more an Expression<Func<T, bool>> but a Func<T, bool>

and AddFilter takes an Expression<Func<T, bool>> as argument.

So... remove the Compile()

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122