0

Delegates can be combined, so:

MethodInvoker del = delegate { Console.WriteLine("Delegate 1"); };
del += delegate { Console.WriteLine("Delegate 2"); };
del.Invoke();

Outputs:

Delegate 1 
Delegate 2

Since it calls both methods, I tried this to see if it would work:

Func<Person, bool> filters = person => person.Name.Contains("John");
filters += person => person.Age >= 18;

List<Person> persons = getPersons();

persons.Where(filters);

But it doesn't, only last is applied (in this case, person => person.Age >= 18).

My question is: Why not? Is it possible to implement this? (not that I would rewrite Linq's extension methods just to implement this, but would be cool if possible)

Note: I know that in LINQ to Objects, doing this would be similar, as execution is deferred:

persons.Where( x => x.Name.Contains( nameFilter )).Where( x => x.Age >= 18 );

But that's not what I'm interested in.

3
  • 1
    Does combining two Func<T, bool> delegates cause them to AND one another? I highly doubt it. Remember that you're actually returning values from these two delegates. I presume everything before the last return value in the invocation list gets discarded. Commented Dec 26, 2011 at 19:35
  • I don't see how it could work: one function says the result is false, the other says the result is true. When actions are combined, they are both executed, but the question of which result to use doesn't come up, because there is no result. Commented Dec 26, 2011 at 19:37
  • 1
    you can write And and Or methods that combine bool-returning delegates the way you want: Func<Person, bool> And(Func<Person, bool> a, Func<Person, bool> b) { return (Person p) => a(p) && b(p); } Commented Dec 26, 2011 at 23:00

2 Answers 2

5

The reason is that both filters are executed, but only the return value of the last delegate is used. That is how delegates work according to the specification. Like the comments say, I can't imagine how this would work differently. You're alluding that the results should be ANDed, but why can't they be ORed? What would happen if the results were strings?

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

Comments

1

This works only with Action delegates, not with Func delegates, since a delegate call can only return one single result.

3 Comments

Great! Would this work with Linq to entities / Nhibernate too? Or depends on the implementation?
@Andre I'm guessing those frameworks are parsing lambda expression trees. If that's the case you could get some weird runtime, not compile time, errors from combining Lambda expressions.
Linq as interface to o/r-mappers works in a completely different way. The lambda expression is not executed at all. Instead, its syntactical structure is analyzed and converted into a SQL statement. Therefore multicasting will not work.

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.