I’m writing some examples to help me use Func<> and Lambda as I need to unravel the previous developers code on the project I am on and he uses techniques that I am not familiar with.
So I’ve written a few examples of increasing complexity from a Simple LINQ query to passing the LINQ as a FUNC to a method. That all seems OK.
Then I switched from LINQ to Lambda. Simple works OK but I can’t figure out how to pass the Lambda as (in?) a Func<>.
My code is below. I’ve marked the bit I’m stuck on as WHAT_GOES_HERE
private void some method()
{
Dictionary<int,string> dic = new Dictionary<int,string>();
//Set the data into the dictionary
dic.Add(1, "Mathew");
dic.Add(2, "Mark");
dic.Add(3, "Luke");
dic.Add(4, "John");
dic.Add(5, "John");
#region Simple LINQ as Lambda
string searchName = "John";
var match4 = dic.Where(entry => entry.Value == searchName).Select(entry => entry.Key);
//Get the data out of the return from the query
foreach (int result in match4)
{
int keyValue = result;
}
#endregion
#region Passing the Simple LINQ as Lambda as a Func<>
//uses the above Lambda - This works although the name used is the value of searchName
IEnumerable<int> match5 = RunTheMethod(deligateName => match4, "John");
//I want to have an inline Lambda as the first parameter
IEnumerable<int> match6 = RunTheMethod(WHAT_GOES_HERE?, "John");
foreach (int result in match6)
{
int keyValue = result;
}
#endregion
}
public IEnumerable<int> RunTheMethod(Func<string, IEnumerable<int>> myMethodName, string name)
{
IEnumerable<int> i = myMethodName(name);
return i;
}
Any thoughts?
Richard
I think you want:
but it’s not entirely clear…