Lambda expression
(input parameters) => expression
(input parameters) => {statement;}
public class Employee
{
public int ID;
public string Name;
public string Addess;
public int Age;
}
List<Employee> ListEmployees = new List<Employee>() { new Employee{ID = 1,Name = "pankaj",Addess="hariodnagar,shivaji park",Age=15}, new Employee{ID = 2,Name = "sano",Addess="shiva,shivaji park",Age=26}, new Employee{ID = 3,Name = "rohit",Addess="nanak nagar,park",Age=35}, new Employee{ID = 4,Name = "rinkal",Addess="naini,cercal",Age=17}, new Employee{ID = 5,Name = "sumit",Addess="har,vaji",Age=42}, new Employee{ID = 6,Name = "rocky",Addess="nagar,jip",Age=66}, new Employee{ID = 7,Name = "pankaj",Addess="nagar,jip",Age=70}, new Employee{ID = 8,Name = "rocky",Addess="nagar,jip",Age=30} }; Response.Write("Employee list are:<br>"); foreach (var item in ListEmployees) { Response.Write("Id " + item.ID +"Name "+ item.Name + "Adress "+ item.Addess + "age "+ item.Age+ "<br>" ); } Response.Write("<br>-------------------------------------------------<br>"); Response.Write("The following code retrieves the first two employee from the list who are older than 35 years:<br>"); //Example 1: The following code retrieves the first two employee from the list who are older than 35 years: foreach (var item in ListEmployees.FindAll(x => x.Age >= 35).Take(2).ToList()) { Response.Write("Name:" + item.Name + "\t Age:" + item.Age); } Response.Write("<br>-------------------------------------------------<br>"); Response.Write("The following code checks any employees's age that is between 13 and 25 years <br>"); //Example 2: The following code checks any employees's age that is between 13 and 25 years ; //return true if satisfied the condition else return false if (ListEmployees.Any(x => (x.Age >= 13 && x.Age <= 19))) { Response.Write("Yes, we have some teen-agers in the list"); } Response.Write("<br>-------------------------------------------------<br><br>"); Response.Write("The following code checks whether all the Employee's ages are greater than Ten years or not: <br>"); //Example 3: The following code checks whether all the Employee's ages are greater than Ten years or not: //its return true if satisfied the condition else return false if (ListEmployees.All(x => (x.Age > 10))) { Response.Write("Yes, all the Employes older than 10 years"); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 4: The following code gets the average of all the Employess's ages: //Returns: The average of the sequence of values. double avgAge = ListEmployees.Average(x => x.Age); Response.Write("The average of all the Employe's age is: " + avgAge); Response.Write("<br>-------------------------------------------------<br><br>"); //Example 5: The following code checks whether a Employe having the name 'SAM' exists or not: // Returns: true if the System.Collections.Generic.List<T> contains one or more elements //that match the conditions defined by the specified predicate; otherwise, false. if (ListEmployees.Exists(x => x.Name == "pankaj")) { Response.Write("Yes, A Employe having name 'pankaj' exists in our list"); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 6: The following code checks at what position a Employe having the name 'rinkal' exists in the list: Response.Write("<br>Checking the index position of a Employe having name 'rinkal' ..."); int indexFor = ListEmployees.FindIndex(x => x.Name == "rinkal"); Response.Write("In the list, The index position of a Employe having name 'rinkal' is : " + indexFor); Response.Write("<br>-------------------------------------------------<br><br>"); //Example 7: The following code retrieves the oldest Employe in the list: Response.Write("<br>Getting the name of the most aged Employe in the list ..."); Employee emp = ListEmployees.First(m => m.Age == (ListEmployees.Max(x => x.Age))); Response.Write("The most aged Employe in our list is: " + emp.Name + " whose age is: " + emp.Age); Response.Write("<br>-------------------------------------------------<br><br>"); //Example 8: The following code gets the total of all the Employee's ages: Response.Write("<br>Getting Sum of all the Employe's age..."); int sumOfAges = ListEmployees.Sum(x => x.Age); Response.Write("The sum of all the Employes's age = " + sumOfAges); Response.Write("<br>-------------------------------------------------<br><br>"); //Example 9: The following code skips each Employe whose age is less than 60: Response.Write("<br>Skipping every Employee whose age is less than 44 years..."); foreach (Employee em in ListEmployees.SkipWhile(x => x.Age < 44)) { Response.Write("Name : " + em.Name + " \t\tAge: " + em.Age); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 10: he following code retrieves all the Employee until we find a Employe with a name beginning with any letter other than "a" : Response.Write("Displaying the Employes until we find a Employe with name starts with other than 'S'"); foreach (Employee em in ListEmployees.TakeWhile(x => x.Name.StartsWith("s"))) { Response.Write("Name : " + em.Name + " \t\tAge: " + em.Age); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 11: The following code checks whether all the Employee have their address or not: Response.Write("<br>Checking all the Employes have SSN or not ..."); if (ListEmployees.TrueForAll(x => x.Addess != null)) { Response.Write("No Employe is found without address"); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 12: The following code removes all the Employee having the name "pankaj": Response.Write("<br>Removing all the Employes record from list that have 'pankaj' name"); ListEmployees.RemoveAll(x => (x.Name == "pankaj")); if (ListEmployees.TrueForAll(x => x.Name != "pankaj")) { Response.Write("No Employe is found with 'pankaj' name in current list<br>"); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 13: The following code searches for the Employe having "5" as their id: Response.Write("<br>Finding the Employe whose id = 5 in the list"); Employee _mployee = ListEmployees.Find(x => (x.ID == 5)); Response.Write("The Employe having id '5' is : " + _mployee.Name + " \t\tAge: " + _mployee.Age); Response.Write("<br>-------------------------------------------------<br><br>"); //Example 14: count number of emp with same name pankaj Response.Write("<br>count number of emp with same name pankaj "); int count = ListEmployees.Count(x => x.Name == "pankaj"); Response.Write("The Employees having same name 'pankaj' is : " + count); Response.Write("<br>-------------------------------------------------<br><br>"); //Example 15: select list of emp where name is rocky // Returns: An System.Collections.Generic.IEnumerable<T> that contains elements from // the input sequence that satisfy the condition. List<Employee> listEmp = ListEmployees.Where(x => x.Name == "rocky").ToList(); int i = 1; foreach (var item in listEmp) { Response.Write("<"+ i++ +"> " + item.Name + "<br>"); } Response.Write("<br>-------------------------------------------------<br><br>"); //Example 16: // Returns: An System.Collections.Generic.IEnumerable<T> whose elements are the result // of invoking the transform function on each element of source. IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x); foreach (int num in squares) { Response.Write(num); } /* This code produces the following output: 1 4 9 16 25 36 49 64 81 100 */ Response.Write("<br>-------------------------------------------------<br><br>");
No comments:
Post a Comment