Reputation: 1055
Say I have the following (incorrect) code;
public void Foo()
{
bool retVal = Bar(x => x.Any(y => y.Contains(z)); // Where z is "my variable" (below)
}
public bool Bar(Func<List<MyObject>, bool> pFunc)
{
return pFunc("a variable");
}
How do I pass the lambda expression, written in the call to Bar, so that it is executed in Bar, using an additional variable that only exists in Bar()?
The code does not have to remain this simple.
Upvotes: 1
Views: 102
Reputation: 2357
You can have two inputs in a lambda: (x,z)
public void Foo()
{
bool retVal = Bar((x,z) => x.Any(y => y.Contains(z))); // Where z is "my variable" (below)
}
public bool Bar(List<MyObject> list, Func<List<MyObject>, string, bool> pFunc)
{
return pFunc(list, "a variable");
}
Which means you have to update the Func signature to Func<List<MyObject>, string, bool>
as well to reflect (x,z)=>bool
I also updated Bar signature to give the input list because it was missing. Maybe it is a local variable or class member. Anyway you should be able to adapt your code starting from here
Upvotes: 3