asp.net mvc框架之扩展方法的使用
在asp.net mvc中数据操作有两种方法
第一种:使用扩展方法
第二种:Linq
本篇博客记录的是使用扩展方法操作数据
class Program { static void Main(string[] args) { Student s1 = new Student() { Id = 1, Name = "张三", Age = 22 }; Student s2 = new Student() { Id = 2, Name = "李四", Age = 33 }; Student s3 = new Student() { Id = 3, Name = "王五", Age = 23 }; Student s4 = new Student() { Id = 4, Name = "小明", Age = 43 }; Student s5 = new Student() { Id = 5, Name = "Amy", Age = 15 }; List<Student> students = new List<Student>() { s1, s2, s3, s4, s5 }; #region Any()扩展方法 //判断集合是否包含元素,返回值是bool类型,一般比Count()>0效率高,Any()还可以指定条件表达式。bool b=list.Any(p=>p.Age>20);等价于bool b=list.Where(p=>p.Age>50).Any(); bool isStudents = students.Any(p => p.Age > 10); Console.WriteLine(isStudents); #endregion #region Where()扩展方法 //条件语句,类似与sql语句中的where条件 var stus = students.Where(p => p.Id > 2); foreach (var stu in stus) { Console.WriteLine(stu.Name); } #endregion #region OrderBy()方法,OrderByDescending()方法 //list.OrderBy(条件表达式);根据表达式进行排序,默认是升序 var stus = students.OrderBy(s => s.Age); foreach (var stu in stus) { Console.WriteLine("学生Id:" + stu.Id + ",学生姓名:" + stu.Name + ",学生年龄:" + stu.Age); } //list.OrderByDescending(条件表达式)降序排序 var stus = students.OrderByDescending(s => s.Id); foreach (var stu in stus) { Console.WriteLine("学生Id:" + stu.Id + ",学生姓名:" + stu.Name); } //指定多个排序规则,不是多个OrderBy的形式,而是OrderBy.ThenByDescending.。。。的形式 var stus = students.OrderByDescending(s => s.Age).ThenByDescending(s => s.Id); #endregion #region Skip(n).Take(m) //Take(n)跳过前n条数据,如果不足n条数据也不会报错。常用来分页获取数据。 //Skip(2).Take(3)表示跳过前2条数据,获取3条数据 var stus = students.Skip(2).Take(3); foreach (var stu in stus) { Console.WriteLine("学生Id:" + stu.Id); } #endregion #region list1.Except(list2)方法 //Except(items1)排除list1集合中的list2集合中存在的元素 List<int> list1 = new List<int>() { 1,2,3,4,5,6,7 }; List<int> list2 = new List<int>() { 4,5,6,7,8,9 }; //去掉list1中含有list2的元素 var except_list1 = list1.Except(list2); foreach (var item in except_list1) { Console.WriteLine(item); } #endregion #region Union(item2)方法 //list1.Union(lists2):把list1集合中的元素和list2集合中的元素组合到一起 List<int> list3 = new List<int>() { 1,2,3,4,5,6,7 }; List<int> list4 = new List<int>() { 4,5,6,7,8,9 }; var list3andlist4 = list3.Union(list4); foreach (var list in list3andlist4) { Console.WriteLine(list); } #endregion #region Intersect(list2)方法 //list1.Intersect(list2)取list1和list2的交集部分 List<int> list5 = new List<int>() { 1,2,3,4,5,6,7 }; List<int> list6 = new List<int>() { 4,5,6,7,8,9 }; var lists = list5.Intersect(list6); foreach (var list in lists) { Console.WriteLine(list); } #endregion #region 分组 GroupBy()方法 Person p1 = new Person() { Id = 1, Name = "张1", Salary = 3000, Age = 20 }; Person p2 = new Person() { Id = 2, Name = "张2", Salary = 3500, Age = 25 }; Person p3 = new Person() { Id = 3, Name = "张3", Salary = 2400, Age = 33 }; Person p4 = new Person() { Id = 4, Name = "张4", Salary = 5200, Age = 20 }; Person p5 = new Person() { Id = 5, Name = "张5", Salary = 3400, Age = 25 }; Person p6 = new Person() { Id = 6, Name = "张6", Salary = 5000, Age = 45 }; List<Person> Persons = new List<Person>() { p1, p2, p3, p4, p5, p6 }; //item.Key表示以什么分组,Key就是什么 var personsByAge = Persons.GroupBy(p => p.Age); foreach (var item in personsByAge) { Console.WriteLine(item.Key + "年龄组:平均工资:" + item.Average(p => p.Salary)); } #endregion #region SelectMany()方法 //SelectMany()把集合中每个对象的另外集合属性的值重新拼接为一个新的集合 Student sd1 = new Student() { Id = 1, Name = "学生1", Age = 15 }; Student sd2 = new Student() { Id = 2, Name = "学生2", Age = 16 }; Student sd3 = new Student() { Id = 3, Name = "学生3", Age = 17 }; Student sd4 = new Student() { Id = 4, Name = "学生4", Age = 14 }; Student sd5 = new Student() { Id = 5, Name = "学生5", Age = 15 }; Student sd6 = new Student() { Id = 6, Name = "学生6", Age = 16 }; Student sd7 = new Student() { Id = 7, Name = "学生7", Age = 19 }; Student sd8 = new Student() { Id = 8, Name = "学生8", Age = 18 }; Student sd9 = new Student() { Id = 9, Name = "学生9", Age = 11 }; Student sd10 = new Student() { Id = 10, Name = "学生10", Age = 13 }; List<Student> list_students = new List<Student>() { sd1, sd2, sd3, sd4, sd5, sd6, sd7, sd8, sd9, sd10 }; Teacher tc1 = new Teacher() { Id = 1, Name = "老师1", Age = 23, stus = { sd3, sd6, sd10 } }; Teacher tc2 = new Teacher() { Id = 2, Name = "老师2", Age = 24, stus = { sd1, sd2, sd4, sd6, sd7, sd9, sd10 } }; Teacher tc3 = new Teacher() { Id = 3, Name = "老师3", Age = 25, stus = { sd1, sd2, sd5, sd8 } }; List<Teacher> list_teachers = new List<Teacher>() { tc1, tc2, tc3 }; //获取每个老师下面的每个学生,形成一个新的集合 var teacher_stus = list_teachers.SelectMany(p => p.stus); foreach (var stus in teacher_stus) { Console.WriteLine(stus.Name); } #endregion #region Join()方法 Master m1 = new Master() { Id = 1, Name = "主人1" }; Master m2 = new Master() { Id = 2, Name = "主人2" }; Master m3 = new Master() { Id = 3, Name = "主人3" }; List<Master> list_masters = new List<Master>() { m1, m2, m3 }; Dog d1 = new Dog() { Id = 1, Name = "田园犬", MasterId = 3 }; Dog d2 = new Dog() { Id = 2, Name = "泰迪", MasterId = 2 }; Dog d3 = new Dog() { Id = 3, Name = "二哈", MasterId = 2 }; Dog d4 = new Dog() { Id = 4, Name = "斗犬", MasterId = 1 }; Dog d5 = new Dog() { Id = 5, Name = "土狗", MasterId = 1 }; List<Dog> list_dogs = new List<Dog>() { d1, d2, d3, d4, d5 }; //联合查询,左表联合右表进行查询。 //list_dogs作为左表,list_masters作为右表,join第一个参数右表,第二个参数左表的字段名,第三个参数右表的字段名,当第二个参数的值等于第三个参数的名字,new的集合是显示的数据字段 var lists=list_dogs.Where(d => d.Id >= 1).Join(list_masters, d => d.MasterId, m => m.Id, (d, m) => new { DogName = d.Name, MasterName = m.Name }); foreach (var list in lists) { Console.WriteLine(list.DogName+","+list.MasterName); } #endregion Console.ReadKey(); } } class Master { public int Id { get; set; } public string Name { get; set; } } class Dog { public int Id { get; set; } public string Name { get; set; } public int MasterId { get; set; } } class Person { public int Id { get; set; } public string Name { get; set; } public int Salary { get; set; } public int Age { get; set; } } class Teacher { public Teacher() { this.stus = new List<Student>(); } public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public List<Student> stus { get; set; } } class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }