1

I am trying to combine a multiple selection with a lambda function into an lambda expression. How do I do that? I know the last line is wrong, but giving you an idea of what I mean.

Func<Event, bool> where = null;

if (!string.IsNullOrWhiteSpace(searchToken))
    where = q => q.Name.ToUpper().Contains(searchToken.ToUpper());

where += q => q.Hidden = false;

Expression<Func<Event, bool>> where1 = q => where; <-- Erroring
5
  • 1
    It's not clear what you're really aiming for. Are you trying to perform multiple filters? What's the bigger picture? Commented Oct 15, 2011 at 22:58
  • The bigger picture is I have a generic function that takes Expression<Func<Event, bool>>. I have conditional statements that I need to add or not. I am using the Func<Event,boo> to build my criteria but want to send that to the generic function as Expression<Func<Event, bool>>. Commented Oct 16, 2011 at 5:26
  • I noticed something called Expression Builder, but is there a default way to do it? Commented Oct 16, 2011 at 5:38
  • You can build an Expression<Func<Event, bool>> from a delegate, but it almost certainly won't do what you need it to. Commented Oct 16, 2011 at 7:27
  • 1
    Note that building a multicast delegate like that almost certainly wouldn't do what you wanted even in LINQ to Objects - only the return value from the last condition would be used. Commented Oct 16, 2011 at 7:28

2 Answers 2

4

I suspect you want PredicateBuilder. (The source is available on that page.) You'd use it like this:

var predicate = q => !q.Hidden;
if (!string.IsNullOrWhiteSpace(searchToken))
{
    predicate = predicate.And(q => q.Name.ToUpper()
                                         .Contains(searchToken.ToUpper());
}
return predicate;

That's assuming you want to "and" the conditions - you never made that clear...

Note that that is not a good way to compare in a case-insensitive way, either. If you could tell us what's going to consume the query (e.g. LINQ to SQL, LINQ to EF) we could suggest a provider-compatible way of performing a case-insensitive query.

Sign up to request clarification or add additional context in comments.

2 Comments

What is var predicate = q => !q.Hidden;? Is that an Expression of Func?
@MikeFlynn: That wouldn't compile - you can't use assign a lambda expression to an implicitly typed variable. You could either have Expression<Func<Foo, bool>> predicate = q => !q.Hidden; or Func<Foo, bool> predicate = q => !q.Hidden;. The first will give you an expression tree, the latter a delegate.
0

Look at http://msdn.microsoft.com/en-us/library/bb882637.aspx. How to use expression trees to build dynamic queries.

AFAIK when using Expression <> like that the expression must be known in compile time, because the compiler then build AST abstract syntax three and stores it as data in your Expression <> instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.