using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyLambda.Extend { /// <summary> /// 扩展方法:静态类 静态方法 第一个参数类型前面加上this /// ExtendShow.ToInt(ivalue);换成 ivalue.ToInt() /// /// /// </summary> public static class ExtendShow { public static int ToInt(this int? iParameter) { return iParameter ?? 0; } public static void NoShow(this LambdaShow show) { Console.WriteLine("这里是扩展的 NoShow"); } public static void Show(this LambdaShow show) { Console.WriteLine("这里是扩展的 NoShow"); } /// <summary> /// /// </summary> /// <param name="object"></param> /// <returns></returns> public static DateTime ToDateTime(this object @object, int a) { return DateTime.Now; } public static DateTime Execute(this Action act) { act(); return DateTime.Now; } public static IEnumerable<TSource> ElevenWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> func) { List<TSource> studentList = new List<TSource>(); foreach (TSource item in source) { bool bResult=func.Invoke(item); if (bResult) { studentList.Add(item); } } return studentList; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyLambda { public delegate void NoReturnNoParaOutClass(); public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, in T17> (T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17); public class LambdaShow { public delegate void NoReturnNoPara();//1 委托的声明 public delegate int WithReturnNoPara(); public delegate void NoReturnWithPara(int id, string name); public delegate LambdaShow WithReturnWithPara(DateTime time); public void Show() { string nameOut = "糯米飞机"; { Student student = new Student(); student.Id = 1; student.Name = "打兔子的猎人"; student.Age = 27; student.ClassId = 2; student.Study(); } { Student student = new Student() { Id = 1, Name = "打兔子的猎人", Age = 27, ClassId = 2 }; student.Study(); } { NoReturnWithPara method = new NoReturnWithPara(this.DoNothing);//2 委托的实例化 method.Invoke(1, "Gain");//3 委托的调用 } { NoReturnWithPara method = this.DoNothing; method.Invoke(2, "憨豆"); } { NoReturnWithPara method = new NoReturnWithPara( delegate(int id, string name) { Console.WriteLine(nameOut); Console.WriteLine("{0} {1} DoNothing"); }); method.Invoke(3, "yoyo"); } { NoReturnWithPara method = new NoReturnWithPara( (int id, string name) =>//goes to //lambda表达式:是一个匿名方法,是一个方法 { Console.WriteLine(nameOut); Console.WriteLine("{0} {1} DoNothing"); }); method.Invoke(4, "Nine"); } { NoReturnWithPara method = new NoReturnWithPara( (id, name) => //去掉参数类型,原因是委托的约束 { Console.WriteLine("{0} {1} DoNothing"); }); method.Invoke(5, "小昶"); } { //如果方法体只有一行,那么可以去掉大括号和分号,多行不行 //如果参数只有一个 可以去掉小括号 //如果没有参数,直接用小括号 NoReturnWithPara method = new NoReturnWithPara((id, name) => Console.WriteLine("{0} {1} DoNothing", id, name)); method.Invoke(6, "梦醒时分"); } { NoReturnWithPara method = (id, name) => Console.WriteLine("{0} {1} DoNothing", id, name); method.Invoke(7, "小石头"); method(8, "Y"); } { //0到16个参数 无返回值的委托 Action act1 = () => Console.WriteLine("1234"); Action<int> act2 = t => { }; Action<int, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> act3 = null; Action<int, NoReturnWithPara, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> act4 = null; } { //0到16个参数 带返回值的委托 Func<int> func1 = () => 1; int i = func1(); Func<int, string> func2 = t => "1234"; Func<int, int, NoReturnWithPara, LambdaShow, LambdaShow, string, string, string, string, string, string, string, string, string, string, string, string> func3 = null; } { //多返回值:1 返回对象 2 ref out WithReturnWithParaRefOut method = new WithReturnWithParaRefOut( delegate(ref DateTime time, out int i) { i = 1; return this; });//lambda表达式不行 } } public delegate LambdaShow WithReturnWithParaRefOut(ref DateTime time, out int i); private void DoNothing(int id, string name) { Console.WriteLine("{0} {1} DoNothing out"); } private void DoNothing1() { Console.WriteLine("DoNothing"); } private void DoNothing2() { Console.WriteLine("DoNothing"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MyLambda.Extend; namespace MyLambda { public class LinqShow { private List<Student> GetStudentList() { #region 初始化数据 List<Student> studentList = new List<Student>() { new Student() { Id=1, Name="打兔子的猎人", ClassId=2, Age=35 }, new Student() { Id=1, Name="Alpha Go", ClassId=2, Age=23 }, new Student() { Id=1, Name="白开水", ClassId=2, Age=27 }, new Student() { Id=1, Name="狼牙道", ClassId=2, Age=26 }, new Student() { Id=1, Name="Nine", ClassId=2, Age=25 }, new Student() { Id=1, Name="Y", ClassId=2, Age=24 }, new Student() { Id=1, Name="小昶", ClassId=2, Age=21 }, new Student() { Id=1, Name="yoyo", ClassId=2, Age=22 }, new Student() { Id=1, Name="冰亮", ClassId=2, Age=34 }, new Student() { Id=1, Name="瀚", ClassId=2, Age=30 }, new Student() { Id=1, Name="毕帆", ClassId=2, Age=30 }, new Student() { Id=1, Name="一点半", ClassId=2, Age=30 }, new Student() { Id=1, Name="小石头", ClassId=2, Age=28 }, new Student() { Id=1, Name="大海", ClassId=2, Age=30 }, }; #endregion return studentList; } public void Show() { List<Student> studentList = this.GetStudentList(); List<Student> studentListlessThan30 = new List<Student>(); foreach (var item in studentList) { if (item.Age < 30) { studentListlessThan30.Add(item); } } { var list = studentList.Where<Student>(s => s.Age < 30);//陈述句 foreach (var item in list) { Console.WriteLine("Name={0} Age={1}", item.Name, item.Age); } } { Console.WriteLine("********************"); var list = studentList.ElevenWhere<Student>(s => s.Age < 30);//陈述句 //Func<Student, bool> func = new Func<Student, bool>(s => s.Age < 30); //var list = ExtendShow.ElevenWhere<Student>(studentList,func);//陈述句 foreach (var item in list) { Console.WriteLine("Name={0} Age={1}", item.Name, item.Age); } } { Console.WriteLine("********************"); var list = from s in studentList where s.Age < 30 select s; foreach (var item in list) { Console.WriteLine("Name={0} Age={1}", item.Name, item.Age); } } { Console.WriteLine("********************"); var list = studentList.Where<Student>(s => s.Age < 30) .Select(s => new { IdName = s.Id + s.Name, ClassName = s.ClassId == 2 ? "高级班" : "其他班" }); foreach (var item in list) { Console.WriteLine("Name={0} Age={1}", item.ClassName, item.IdName); } } { Console.WriteLine("********************"); var list = from s in studentList where s.Age < 30 select new { IdName = s.Id + s.Name, ClassName = s.ClassId == 2 ? "高级班" : "其他班" }; foreach (var item in list) { Console.WriteLine("Name={0} Age={1}", item.ClassName, item.IdName); } } { Console.WriteLine("********************"); var list = studentList.Where<Student>(s => s.Age < 30) .Select(s => new { Id = s.Id, ClassId = s.ClassId, IdName = s.Id + s.Name, ClassName = s.ClassId == 2 ? "高级班" : "其他班" }) .OrderBy(s => s.Id) .OrderByDescending(s => s.ClassId) .Skip(2) .Take(3) ; foreach (var item in list) { Console.WriteLine("Name={0} Age={1}", item.ClassName, item.IdName); } } { List<Class> classList = new List<Class>() { new Class() { Id=1, ClassName="初级班" }, new Class() { Id=2, ClassName="高级班" }, new Class() { Id=3, ClassName="微信小程序" }, }; var list = from s in studentList join c in classList on s.ClassId equals c.Id select new { Name = s.Name, CalssName = c.ClassName }; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyLambda { public class Student { public int Id { get; set; } public int ClassId { get; set; } public string Name { get; set; } public int Age { get; set; } public void Study() { Console.WriteLine("{0} {1}跟着Eleven老师学习.net高级开发", this.Id, this.Name); } } public class Class { public int Id { get; set; } public string ClassName { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MyLambda.Extend; namespace MyLambda { /// <summary> /// 1 委托简介 /// 2 匿名方法 匿名类 var /// 3 lambda表达式 goes to /// 4 系统自带委托Action/Func /// 5 扩展方法 /// 6 linq扩展 /// 7 linq简单回顾 /// 8 作业部署 /// </summary> class Program { //private var name = ""; static void Main(string[] args) { try { Console.WriteLine("=============================今天是lambda/linq课程========================================="); { LambdaShow show = new LambdaShow(); show.Show(); show.NoShow(); show.ToDateTime(1); } #region MyRegion { var model = new { Id = 1, Name = "Alpha Go", Age = 21, ClassId = 2 }; Console.WriteLine(model.Id); Console.WriteLine(model.Name); //model.Name = "张伟东"; Student student = new Student() { Id = 1, Name = "打兔子的猎人", Age = 27, ClassId = 2 }; var name = ""; Console.WriteLine(name); //var id; } #endregion #region MyRegion { int? ivalue = null; int? ivalue2 = 1; ivalue.ToDateTime(1); ivalue2.ToDateTime(1); int i = ExtendShow.ToInt(ivalue); int i2 = ivalue.ToInt(); new Action(() => { }).Execute(); } #endregion #region linq { LinqShow show = new LinqShow(); show.Show(); } #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } } }