MVC(基础一)
MVC学习之前必须掌握的c#基础知识
一.类自动属性
public class Person { //自动属性 public string Name { get; set; } private int _age; public int age { get { return _age; } set { _age = value; } } }
Name为类的自动属性,其实跟下面age的定义方法是一样的,只不过是微软自动帮你生成了字段。
二.隐式类型
static void Var() { var person = new Person(); var p = person; }
1.var类型会被编译器根据初始值的类型自动推断出具体的类型。
2. var类型不能做参数。
3.无法将null赋值给var类型。
4.语句中只声明一次变量,声明后不能更改类型。
三、匿名类
static void W02AnmClass() { var d = new { name = "张三", age = 13 }; var d2 = new { name = 12, age = "13" }; var d3 = new { name = "张三", age = "13", gender="男"}; }
1.匿名类本质上是编译器生成的一个泛型类。
2.当匿名类的参数个数一样的时候是共用一个泛型类。
3.当匿名类的参数个数不一样的时候是各自生成不同的泛型类。
4.匿名类初始化的时候一定要赋值。
四、默认值和命名参数
static void W03DefaultValue() { W0301(); W0301("李四"); W0301(age: 20); } static void W0301(string name="张三",int age=18) { Person person = new Person(); person.Name = name; person.Age = age; }
1.当一个方法里面的参数有默认值的时候,可以直接调用方法不用传值,编译器会自动调用参数的值。
2.当方法有默认值时,如果调用的时候只传入第一个参数,那么编译器会把第二个参数设置为默认值。
3.如果需要给某一个参数赋值,书写的格式是 参数名:值。
五、对象/集合初始化器
static void W0401Object() { Person person = new Person() { Name="张三", Age=23 }; } static void W0402Object() { var personlist = new List<Person>() { new Person() {Name="张三",Age=22 }, new Person() {Name="李四",Age=12 } }; } static void W0403Object() { Person[] arrperson = new Person[] { new Person() {Name="张三",Age=22 } }; }
1.对象/集合初始化器,可以在new一个对象或者集合的时候,直接在后面加{}里面对属性直接赋值或者直接new一个对象给集合。
六、匿名方法(匿名函数、匿名委托)
static bool Process(int p) { return p > 2; } static void W0401() { List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //直接传入一个方法名称也可以不过这个方法名称的参数以及返回值类型要跟 //public delegate bool Predicate<in T>(T obj);一样 var nlist =list.FindAll(Process); foreach (var item in nlist) { Console.WriteLine(item); } Console.WriteLine("================="); //匿名方法的写法:delegate(编写匿名方法的参数,参数有程序员自己定义){函数的方法体代码} var nlist2 = list.FindAll(delegate (int p) { return p > 3; }); foreach (var item in nlist2) { Console.WriteLine(item); } }
七、lambda表达式的推断以及书写方法
List<Person> persons = new List<Person>() { new Person { Name="张三",Age=12}, new Person { Name="李四",Age=32}, new Person { Name="王五",Age=44} }; //使用匿名方法返回符合条件的数据。 var nlistperson= persons.FindAll(delegate (Person person) { return person.Age > 22 && person.Name.Contains("李"); }); //lambda表达式的推断使用goto语句来替换匿名函数 => var nlistperson2 = persons.FindAll((Person person)=> { return person.Age > 22 && person.Name.Contains("李"); }); /* 1、lambda表达式的推断使用goto语句来替换匿名函数 => 继续简化 2、当只有一个参数的时候可以省略()参数的括号,当返回值只有一句语句的时候可以省略return以及{}花括号 3、当有多个参数的时候需要加上(),当有多条语句的时候需要添加{}每个语句后面加分号;最后返回的语句要加return */ var nlistperson3 = persons.FindAll(person => person.Age > 22 && person.Name.Contains("李")); var nlistperson4 = persons.FindAll(person => { person.Age++; return person.Age > 22 && person.Name.Contains("李"); }); foreach (var item in nlistperson4) { Console.WriteLine("名称:{0};年龄:{1}",item.Name,item.Age); }
八、扩展方法
/// <summary> /// 1.扩展方法必须是一个静态方法 /// 2.静态方法必须放在静态类中 /// 3.扩展方法第一个参数必须是this开头,并且指定扩展方法是哪个类型上的 /// 4.扩展方法只能有指定的类型才能点出来 /// 5.扩展方法的this后面的参数不属于 方法的参数 /// 6.如果扩展方法跟实例方法同名,先调用实例方法 /// 7.子类可以调用父类的扩展方法 /// 8.接口上的扩展方法可以被实现类的对象直接使用 /// 9.扩展方法的本质,最终还是被编译器编译成了 静态类.静态方法() /// </summary> static void W0601() { DateTime dt = DateTime.Now; string fmtstr= dt.FmtDate(); }
public static class ExtHelper { public static string FmtDate(this DateTime dt) { return dt.ToString("YYYY-MM-DD HH:MM:ss"); } public static string FmtDate(this DateTime dt,string str) { return dt.ToString("YYYY-MM-DD HH:MM:ss"+str); } }
九、系统内置的委托
#region 系统内置委托 #region Action委托 接收参数 无法回值 static void W0701Action() { List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 }; list.ForEach(i => Console.WriteLine(i)); } #endregion #region Predicate 泛型委托:接收一个参数,返回bool类型 static void W0702Predicate() { var list = new List<Person>(){ new Person { Name = "张三", Age = 12 }, new Person { Name = "李四", Age = 32 }, new Person { Name = "王五", Age = 44 } }; var nlist = list.FindAll(c => c.Age > 22); //获取当前list集合中,名字为张三的对象,如果有多个只返回第一个 var person = list.Find(c => c.Name == "张三"); } #endregion #region Func:接收参数,返回参数,但是不固定 static void W0703Func() { var list = new List<Person>(){ new Person { Name = "张三", Age = 12 }, new Person { Name = "李四", Age = 32 }, new Person { Name = "王五", Age = 44 } }; var nlist = list.Where(c => c.Age > 1); var nlist1 = nlist.ToList(); nlist1.ForEach(c => Console.WriteLine(c.Name.ToString())); } #endregion #region System.Comparison<T> 返回一个整数,接收两个同类型的参数 static void W0704Comparison() { var list = new List<Person>(){ new Person { Name = "张三", Age = 12 }, new Person { Name = "李四", Age = 32 }, new Person { Name = "王五", Age = 44 } }; list.Sort((p, n) =>p.Age - n.Age); list.ForEach(c=>Console.WriteLine(c.Name.ToString()+":"+c.Age.ToString())); } #endregion #endregion
十、SQO方法 -标准查询运算符
创建两个类
public class Person { public int TypeID { get; set; } public int ID { get; set; } //自动属性 public string Name { get; set; } private int _age; public int Age { get { return _age; } set { _age = value; } } public override string ToString() { return "ID=" + this.ID + "名称=" + this.Name + ",年龄=" + this.Age + "所属类别=" + this.TypeID; } } public class PsersonType { public int TypeID { get; set; } public string TName { get; set; } }
SQO常用方法的使用 查询、排序、关联、分组等
#region SQO方法 -标准查询运算符 #region 1 where() firstordefult() LastOrDefault方法进行查找操作 (链式编程) static void W0801() { var list = new List<Person>(){ new Person { Name = "张三", Age = 12 }, new Person { Name = "李四", Age = 32 }, new Person { Name = "王五", Age = 44 } }; //利用where查找出list中年龄大于22或者名称叫王五 //链式编程 list.Where(c => c.Age > 5 || c.Name == "王五").ToList().ForEach(c => Console.WriteLine(c.ToString())); //firstordefult()的演示 var person= list.FirstOrDefault(c => c.Age > 5 || c.Name == "王五"); Console.WriteLine(person.ToString()); //获取集合中满足条件的最后一个元素 var person2 = list.LastOrDefault(c => c.Age > 5 || c.Name == "王五"); Console.WriteLine(person.ToString()); } #endregion #region 2 对集合进行排序(正序和倒序) static void W0802() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1} }; //正序 list.OrderBy(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString())); Console.WriteLine("---------"); //倒序 list.OrderByDescending(c => c.Age).ToList().ForEach(c => Console.WriteLine(c.ToString())); } #endregion #region 3 根据多个字段的组合排序 static void W0803() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1} }; //多个条件进行正序 先按照年龄然后按照ID正序 list.OrderBy(c => c.Age).ThenBy(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString())); //多条件倒序 list.OrderByDescending(c => c.Age).ThenByDescending(c => c.ID).ToList().ForEach(c => Console.WriteLine(c.ToString())); } #endregion #region 4 投影方法(Select) ( 重点 ) static void W0804() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1} }; //取出list集合里面的部分字段 List<string> nlist = list.Select(c => c.Name).ToList(); //重新创建一个新的list集合只包含name和age list.Select(c => new { N = c.Name, A = c.Age }).ToList().ForEach(c => Console.WriteLine(c.ToString())); } #endregion #region 5 分页方法 Skip(skipNum).Take(页容量) Skip():表示跳过集合中的前面多少行 static void W0805() { int rowcount = 0; W0805ByPage(1, 2, out rowcount).ForEach(c => Console.WriteLine(c.ToString())); } static List<Person> W0805ByPage(int pageindex, int pagesize, out int rowcount) { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1} }; // 给rowcount赋予满足条件的数据总行数 rowcount = list.Count(); //经过第几页跳过跳过多少条 int skipCount = (pageindex - 1) * pagesize; return list.Where(c => c.Age > 1).Skip(skipCount).Take(pagesize).ToList(); } #endregion #region 6 连表查询Join static void W0806() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3} }; var typelist = new List<PsersonType>() { new PsersonType(){TypeID=1,TName="老师"}, new PsersonType(){TypeID=3,TName="学生"}, new PsersonType(){ TypeID=2, TName="保安"} }; //利用SQO方法Join() 来实现上述业务逻辑 var nlist = list.Join(typelist, p => p.TypeID, c => c.TypeID, (p, c) =>new { p.Name,p.Age,c.TName }).ToList(); nlist.ForEach(c => Console.WriteLine(c.ToString())); } #endregion #region 7 分组 GroupBy() static void W0807() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3} }; //利用SQO方法进行分组操作 list.GroupBy(c => c.TypeID).ToList().ForEach(c => { Console.WriteLine("----分组号{0}" + c.Key,c.ToList().Count()); c.ToList().ForEach(g => Console.WriteLine(g.ToString())); }); } #endregion #endregion
十一、linq的常用方法
#region Linq使用方法 #region 1 Linq实现排序 static void w0901() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3} }; //需求:根据list集合中的age倒序排列后取出pig对象中的Name和Age //ascending :正序 descending:倒序 var orderbyList = (from c in list orderby c.Age descending select new { c.Name, c.Age }).ToList(); orderbyList.ForEach(c => Console.WriteLine(c.ToString())); } #endregion #region 2 LinqJoin实现集合的链接查询 static void W0902() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3} }; var typelist = new List<PsersonType>() { new PsersonType(){TypeID=1,TName="老师"}, new PsersonType(){TypeID=3,TName="学生"}, new PsersonType(){ TypeID=2, TName="保安"} }; var nlist = (from c in list join t in typelist on c.TypeID equals t.TypeID select new { c.Name, c.ID, t.TName }).ToString(); } #endregion #region Linq分组 static void W0903() { var list = new List<Person>(){ new Person {ID=1, Name = "张三", Age = 12,TypeID=1 }, new Person {ID=2,Name = "李四", Age = 32 ,TypeID=2}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=1}, new Person {ID=3,Name = "王五", Age = 44 ,TypeID=3} }; var groupbylist = (from c in list group c by c.TypeID).ToList(); groupbylist.ForEach(c => { Console.WriteLine("-----分组号=" + c.Key); c.ToList().ForEach(g => Console.WriteLine(g.ToString())); }); } #endregion #endregion