Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend predicate in lambda expression parameter

I would like to extend the expression parameter in my method to add my own filters. I am trying to do something like below, but the syntax is wrong:

public static IList<MyPage> DoSomething<T>(Expression<Func<T, bool>> predicate)
{
    return DataStore().GetPages().Where(p => p.PublishDate < DateTime.Now && predicate)
}

The compiler is complaining is complaining in Visual Studio 2012 with this error:

Error 29 Operator '&&' cannot be applied to operands of type 'bool' and 'System.Linq.Expressions.Expression<System.Func<T,bool>>'

Would extending the predicate first be better then feed if back as .Where(predicate)? How would you do that?

like image 319
TruMan1 Avatar asked Mar 09 '26 01:03

TruMan1


1 Answers

Would extending the predicate first be better then feed if back as .Where(predicate)? How would you do that?

Yes, and exactly like that, if I understand what you are suggesting correctly. You can chain .Where() like this:

public static IList<MyPage> DoSomething<T>(Expression<Func<T, bool>> predicate)
{
    return DataStore().GetPages().Where(p => p.PublishDate < DateTime.Now).Where(predicate);
}