diff --git "a/\347\275\227\345\251\267/2024.12.09\347\254\224\350\256\260.md" "b/\347\275\227\345\251\267/2024.12.09\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..3c1ad93169225a600b089420e4ad9e67266ad992 --- /dev/null +++ "b/\347\275\227\345\251\267/2024.12.09\347\254\224\350\256\260.md" @@ -0,0 +1,35 @@ +应用EntityFrameworkCore的步骤 +安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +定义数据库表模型 + + +namespace Blog.Models; + +public class Blogs +{ + public int Id {get; set;} + public string Title {get; set;} = null!; + public string Content {get; set;} = null!; + public string Author {get; set;} = null!; +} + +定义数据库上下文 + +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set;} = null!; + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=.\\SQLEXPRESS;database=Blog;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} + +生成迁移文件,,命令:dotnet ef migrations add XXX (PS: 可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +将上一步生成的迁移文件,更新到数据库:dotnet ef database update(PS: 需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) \ No newline at end of file diff --git "a/\347\275\227\345\251\267/2024.12.12\347\254\224\350\256\260.md" "b/\347\275\227\345\251\267/2024.12.12\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..ee46539aa70f3717291706bc885d7f859493fd01 --- /dev/null +++ "b/\347\275\227\345\251\267/2024.12.12\347\254\224\350\256\260.md" @@ -0,0 +1,194 @@ + +### Linq查询作业 +基础练习 + + public IActionResult One() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + //1.查询特定元素 找出数组中等于5的元素 + var a = numbers.Where(x => x == 5); + //2.查询特定范围的元素 找出数组中在2到8之间的元素 + var b = numbers.Where(x => x >= 2 && x <= 8); + + ViewBag.a = string.Join("",b); + //3.查询并转换元素 将数组中的每个数字乘以2 + var c=numbers.Select(x=>x*2); + return View(); + } + public IActionResult Two() + { + //4.查询特定属性的对象 找出所有名字以"王"开头的学生 + List students = new List + { + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "王中王", Age = 22 }, + new Student {Id=3, Name = "张语嫣", Age = 23 }, + new Student {Id=4, Name = "詹宇航", Age = 35 }, + new Student {Id=5, Name = "郑雨良", Age = 26 }, + }; + var d = students.Where(x => x.Name.StartWith("王")); + return d; + } + public IActionResult Three() + { + List students = new List + { + //5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, + }; + var e = students.Where(n=>n.Age>=20).OrderByDescending; + return e; + } + public IActionResult Four() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + //6.查询并去重 找出数组中所有不重复的数字。 + var a=numbers.Distinct(); + //7.查询第一个元素 找出数组中第一个大于3的元素。 + var b=numbers.FirstOrDefault(x=>x>3); + //8.查询最后一个元素 找出数组中最后一个小于7的元素。 + var c=numbers.LastOrDefault(x=>x<7); + //9.查询元素是否存在 检查数组中是否存在大于10的元素 + var d=numbers.Any(x=>x>10); + //10.查询元素的计数 计算数组中大于5的元素数量。 + var e=numbers.Count(x=>x>5); + //11.查询元素的总和 计算数组中所有元素的总和。 + var f=numbers.Sum(); + //12.查询元素的最大值 找出数组中的最大值。 + var g=numbers.Max(); + //13.查询元素的最小值 找出数组中的最小值。 + var h=numbers.Min(); + //14.查询元素的平均值 计算数组中所有元素的平均值。 + var i=numbers.Average(); + //15.查询特定条件的元素 找出数组中能被3整除的元素。 + var j=numbers.Where(n => n % 3 == 0); + return View(); + } +中级练习 + public IActionResult One() + { + List students = new List + { + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, + }; + List courses=new List + { + new Course{StudentId=1,CourseName="英语"}, + new Course{StudentId=1,CourseName="数学"}, + new Course{StudentId=2,CourseName="语文"}, + new Course{StudentId=3,CourseName="物理"}, + new Course{StudentId=4,CourseName="化学"}, + new Course{StudentId=4,CourseName="生物"}, + new Course{StudentId=4,CourseName="语文"}, + }; + //16.查询并选择特定属性 找出所有学生的姓名和年龄 + var a = students.Select(x => new {x.Name,x.Age}); + //Age = x.Age+1 这个可以拿来计算农历年龄 + //老胡上课说的,这里也给写上吧。(我就是不想单独整理,怎么了,打死我?) + var res = stujdents.Select(x => { + var tmpName = x.Age <= 22 ? $"{x.Name}小朋友":$"{x.Name}先生";//没错,是的,我就是先生 + var tmpAge = x.Age + 1; + return new {Name = tmpName,Age = tmpAge}; + }); + return res; + //17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + var b=students.GroupBy(s => s.Age) .Select(g => new { Age = g.Key, Count = g.Count()}); + return View(); + //18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 + var a = from student in students join course in courses on student.Id equals course.StudentId into studentCourses + select new{StudentName = student.Name,Courses = studentCourses.Select(c => c.CourseName).ToList()}; + return View(a.ToList()); + } + public IActionResult Three() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + //19.查询并反转 反转数组中的元素顺序。 + var a = numbers.AsEnumerable().Reverse(); + int[] four = a.ToArray(); + //20.查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + int b = numbers.FirstOrDefault(n => n > 2); + if (b != 0) + { + int index = Array.IndexOf(numbers, b); + for (int i = index + 1; i < numbers.Length; i++) + { + numbers[i] = b; + } + } + //21.查询并排除 从数组中排除所有小于5的元素。 + var c = numbers.Where(n => n >= 5); + return View(); + } + public IActionResult Four() + { + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + //22.查询并填充默认值 如果数组中存在null值,用默认值0替换。 + var a = nullableNumbers.Select(n => n ?? 0); + return View(); + } + public IActionResult Five() + { + string[] stringNumbers = { "1", "2", "3", "4" }; + //23.查询并转换类型 将字符串数组转换为整数数组。 + int[] a = stringNumbers.Select(int.Parse).ToArray(); + return View(); + } + public IActionResult Six() + { + object[] objects = { "String", 123, "Another String", 456 }; + var result = objects.OfType(); + //24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + return View(); + } +高级练习 + public IActionResult One() + { + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + //25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + var a = numbers1.Zip(numbers2, (first, second) => new { first, second }); + return View(); + } + public IActionResult Two(){ + //26.查询并使用Range生成 生成一个包含1到10的整数数组 + int[] a = Enumerable.Range(1, 10).ToArray(); + return View(a); + } + public IActionResult Three(){ + //27.查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + // 重复的元素 + int number = 7; + // 重复的次数 + int repeatCount = 10; + // 使用LINQ的Repeat方法重复元素 + var a = Enumerable.Repeat(number, repeatCount).ToArray(); + return View(); + } + public IActionResult Four(){ + //28.查询并使用Take限制数量 从数组中取出前5个元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ的Take方法取出前5个元素 + var a = numbers.Take(5).ToArray(); + return View(); + } + public IActionResult Five(){ + //29.查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ的Skip方法跳过前3个元素 + var a = numbers.Skip(3).ToArray(); + return View(); + } \ No newline at end of file