前言
1、"Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量,lambda表达式简化了匿名委托的使用。
2、它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。
简洁
lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 “成为”。运算符将表达式分为两部分,左边指定输入参数,右边是lambda的主体。
lambda表达式:
1.一个参数:param => expr
2.多个参数:(param, list) => expr
namespace study_lambda { /// <summary> /// 委托跟表达式的两段代码,我们可以看出一些东东吧:其实表达式(p => p + 10;)中的 p 就代表委托方法中的参数,而表达式符号右边的 p+10,就是委托方法中的返回结果。 /// </summary> class LambdaPrinciple { delegate int HuaQian(int money); // 委托 void Test() { HuaQian hq = HqTest; int money = hq(10); } public int HqTest(int money) { return money + 10; } //表达式 void Test2() { HuaQian hq = p => p + 10; int money = hq(10); } delegate int HuaQian1(int money, int money2); //多参数 void Test3() { HuaQian1 hq = (p, m) => p + m; int money = hq(1, 2); } //lambda主体运算复杂 void Test4() { HuaQian1 hq = (p, m) => { if (p < 0) { return m; } else { return p + m; } }; } } }
using System.Collections.Generic; namespace study_lambda { class Employee { public string Name { get; set; } public string Code { get; set; } public int Age { get; set; } public Employee(string name, string code, int age) { Name = name; Code = code; Age = age; } public static List<Employee> CreateEmployees() { List<Employee> employees = new List<Employee>(); Employee employee = new Employee("gongyg", "SC0001", 29); employees.Add(employee); employee = new Employee("gumd", "SC0002", 31); employees.Add(employee); employee = new Employee("gongxy", "SC0003", 3); employees.Add(employee); employee = new Employee("gongxh", "SC0004", 1); employees.Add(employee); employee = new Employee("gongyg", "SC0005", 29); employees.Add(employee); return employees; } public override string ToString() { return Name + "-" + Code + "-" + Age; } } class Score { public string Code { get; set; } public string Project { get; set; } public int ProjectScore { get; set; } public Score(string code, string project, int projectScore) { Code = code; Project = project; ProjectScore = projectScore; } public static List<Score> CreateScores() { List<Score> scores = new List<Score>(); Score score = new Score("SC0001", "数学", 89); scores.Add(score); score = new Score("SC0001", "语文", 49); scores.Add(score); score = new Score("SC0001", "英语", 29); scores.Add(score); score = new Score("SC0002", "数学", 68); scores.Add(score); score = new Score("SC0003", "数学", 73); scores.Add(score); score = new Score("SC0004", "数学", 56); scores.Add(score); return scores; } } } using System.Collections.Generic; using System.Linq; namespace study_lambda { /// <summary> /// List集合中的Lambda表达式的运用 /// </summary> class Program { static void Main(string[] args) { List<Employee> employees = Employee.CreateEmployees(); List<Score> scores = Score.CreateScores(); //查询 List<Employee> emps = employees.Where(p => p.Age > 20 || p.Name == "gongxy" || p.Name.Contains("g")).ToList(); //返回第一个符合条件的数据,不存在的时候返回Null。 //当查询条件能查到多个时,直接报异常 Employee emp = employees.SingleOrDefault(p => p.Name == "gumd1"); //返回第一个符合条件的数据,不存在的时候返回Null。 Employee emp2 = employees.FirstOrDefault(p => p.Name == "gongyg"); //排序 //string.Join(',', emps2) //OrderBy 从小到大 //OrderByDescending 从大到小 List<Employee> emps2 = employees.OrderBy(p => p.Age).ToList(); List<Employee> emps3 = employees.OrderByDescending(p => p.Age).ToList(); //返回符合条件的实体个数 int employeeNumber = employees.Count(p => p.Age >= 20); //查找所有名字中含有【g】的实体集合 List<Employee> emps4 = employees.FindAll(p => p.Name.Contains("g")).ToList(); List<IGrouping<string, Employee>> strs = employees.GroupBy(p => p.Name).ToList(); foreach (IGrouping<string, Employee> item in strs) { string key = item.Key; } //返回最大的年龄 int age = employees.Max(p => p.Age); //对所有年龄求和 int ageSum = employees.Sum(p => p.Age); //求年龄的平均值 double ageAvg = employees.Average(p => p.Age); //获取所有学生姓名,并去重 List<string> strs2 = employees.Select(p => p.Name).Distinct().ToList(); //匿名类型 var emps5 = employees.Select(p => new { p.Name, p.Code }); foreach (var item in emps5) { string name = item.Name; string code = item.Code; } } } }