List <T> .Find (Predicates / Lambda)
Can someone tell me what is the difference / advantage between the following 3 Search Options:
List<Employee> Employees = new List<Employee>();
Employee tmp = new Employee();
tmp.FirstName = "Randy";
tmp.LastName = "Jones";
Employees.Add(tmp);
tmp.FirstName = "David";
tmp.LastName = "Smith";
Employees.Add(tmp);
tmp.FirstName = "Michele";
tmp.LastName = "Morris";
Employees.Add(tmp);
// Find option 1
Employee eFound1= Employees.Find((Employee emp1) => {return emp1.LastName == "Jones";});
// Find option 2
Employee eFound2 = Employees.Find(emp2 => emp2.LastName == "Smith");
// Find option 3
Employee eFound3 = Employees.Find(
delegate(Employee emp3)
{
return emp3.LastName == "Morris";
}
);
I read about lambdas and predicates (which I suspect is somehow related to the answer), but I can't put it all together. Any enlightenment will be appreciated!
Thanks david
+4
2 answers
. , int bool. , :
var numbers = Enumerable.Range(1,10);
var predicate = new Predicate<int>(pairNumber);
var pair_numbers = list.FindAll(predicate);
, :
bool pairNumber(int arg)
{
return (arg % 2 == 0);
}
lambdas :
var numbers = Enumerable.Range(1,10);
var pair_values = list.FindAll(val => val % 2 == 0);
.
, , , . , :)
+1