Lambda expressions are part of .Net 3.0. They are used extensively in Linq, but they can also be used on their own. With Lambda expressions, filtering and sorting Lists has become a lot easier.
In my examples, I'm going to filter and sort Lists of Employee objects:
public class Employee{ public string FirstName {set; get;} public string LastName {set; get;} public decimal Salary {set; get;} public bool IsManager {set; get;}}
List<Employee> managers = new List<Employee>( );foreach (Employee employee in employees){ if (employee.IsManager == true) managers.Add(employee);}
List<Employee> managers = employees.FindAll(employee => employee.IsManager == true);
List<Employee> managers = employees.FindAll(e => e.IsManager == true);
using System.Linq;foreach (Employee employee in employees.Where(e => e.IsManager == true)){ // do something}
Employee firstManager = employees.Find(employee => employee.IsManager == true);if (firstManager == null) Console.WriteLine("No managers found in list.");
employees.ForEach(e => e.Salary += 100);
using System.Linq; List<Employee> sortedList = employees.OrderBy(e => e.Age).ToList();
using System.Linq;
List<Employee> sortedList = employees.OrderBy(e => e.Age).ToList();
employees = employees.OrderBy(e => e.Age).ToList();
List<Employee> sortedList = employees.OrderByDescending(e => e.Age).ToList();
employees.Sort((e1, e2) => e1.DOB.CompareTo(e2.DOB));
employees = employees.Sort((e1, e2) => e1.DOB.CompareTo(e2.DOB));
public enum SortOrder { Ascending, Decending }
public static void Sort<TKey>(ref List<Employee> list, Func<Employee, TKey> sortKey, SortOrder sortOrder){ if (sortOrder == SortOrder.Ascending) list = list.OrderBy(sortKey).ToList(); else list = list.OrderByDescending(sortKey).ToList();}
Employee.Sort(employees, e => e.Salary, SortOrder.Ascending);
Employee.Sort(employees, e => e.LastName, SortOrder.Descending);
Employee
.Sort(employees, e => e.LastName, SortOrder.Descending);
Lambda Expressions are Wonderful
Method Parameter Types in C#
Perhaps you need to try fewer languages and get a better handle on the one or two that you prefer to use the most. If you are doing anything with MS and .Net, Lambda Expressions are here to stay and do indeed make life a little more simple and code mush easier to read.
Good article and easy to understand. Thanks.
This article is an example of why I like C# Corner. It's simple and easy to follow, so I can get a quick introduction to the topic without time-consuming complexity. Well done!==============The feedback mechanism for this site could be improved, however. The User ID is case-sensitive, which is unusual. Also when I submitted my comment, the site asked me to log in, the ID I typed didn't match the original case, so was rejected. I selected the "forgot password" link and got the password, BUT THE SITE THREW AWAY THE COMMENTS I HAD ENTERED! This wastes peoples' time.
I did not learn much from this article even though I have 3 years of coding in many languages and databases. I was not able to understand much but I have many years coding and work in many fields.
Man, I have been serching for something like this for a long time.You saved me countless hours of coding.Thank you Andy, good work!Ping-Fu