How to extend predicate in lambda expression parameter

.net c# expression-trees lambda linq

Question

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?

1
3
2/23/2013 9:48:42 AM

Accepted Answer

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);
}
4
2/23/2013 9:53:21 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow