Problems with passing Lambda LINQ statement as Func<>
I'm writing some examples to help me use Func<> and Lambda as I need to unravel the p开发者_StackOverflow中文版revious 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:
RunTheMethod(name => dic.Where(entry => entry.Value == name)
.Select(entry => entry.Key),
"John");
but it's not entirely clear...
In your code match4
is not a lambda, it's just IEnumerable<int>
. I guess you want something like
var selector = searchName => dic.Where(entry => entry.Value == searchName)
.Select(entry => entry.Key);
var ints = selector("John");
Annonymous delegate example:
IEnumerable<int> match6 = RunTheMethod((name) =>
{
//do something with name
return new List<int>();
}, "John");
Your Func<string, IEnumerable<int>>
type means that it's expecting a delegate to a method that takes a string for the first parameter and will return a IEnumerable<int>
in the syntax (name) => {}
name is the argument name and can be accessed inside of the braces {}.
You need to put a lambda expression there that matches: Func<string, IEnumerable<int>>
. Takes a String
, and returns an IEnumerable<int>
. It could be anything. For example:
S => S.Chars("T").Select(C => C.Length)
...would be valid. This would return an IEnumerable of the length of each string, if the given string was split into smaller strings delineated by the T character.
Not what you probably want to do, just an example. My guess is that you don't want to solve your problem this way, but this is the type of thing that would need to be at the spot you indicate. Any lambda expression where a String
is turned into an IEnumerable<T>
Func<string, IEnumerable<int>> searchFunc = (searchValue) => dic.Where(entry => entry.Value == searchValue).Select(entry => entry.Key);
IEnumerable<int> match6 = RunTheMethod(searchFunc, "John");
Hope this will answer your problem
精彩评论