C# List.Find method - how can I pass a value into the predicate?
c#, collections, find, list, predicate
Solution
Use a lambda:
Uri u = new Uri("www.test.com");
CustomClass cc = this.files.Find(cc => cc.Path == u);
or if you still want a named method:
static bool matchesUri(CustomClass cc, Uri _u)
{
return cc.Path == _u;
}
Uri u = new Uri("www.test.com");
CustomClass cc = this.files.Find(cc => matchesUri(cc, u));
Problem
I can't work out how to do a "find" on a List I have based on use of a value that I'll pass in at run time. If you see my below code, I want to be able to find the CustomClass in the List for which it's Path parameter is equal to X, where X will be defined at run time. Any ideas how to do such a find on a List? Or is this not possible without writing an iterator and doing the find manually? In which case perhaps there is a key'ed collection I should look at using instead? ``` private List<CustomClass> files; public void someMethod() { Uri u= new Uri(www.test.com); CustomClass cc = this.files.find( matchesUri(u) ); // WON'T LET ME DO THIS } private static bool matchesUri(List<CustomClass> cc, Uri _u) { return cc.Path == _u; } public class CustomClass { private Uri path; public Uri Path { get { return this.path; } set { this.path = value; } } } ``` PS. I must admit I don't quite follow the predicate stuff in the doco at http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx