From c3f2922ae20be96017cf14afaf2761ecd8917d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E6=9D=A8=E6=82=A6?= <14091313+ding-yangyue@user.noreply.gitee.com> Date: Fri, 13 Dec 2024 20:58:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2024-11-14 mvc\345\212\240\345\274\272.md" | 57 --------------- ...40\351\200\222\345\217\202\346\225\260.md" | 45 ------------ ...04\346\214\201\344\271\205\345\214\226.md" | 35 +++++++++ ...024-12-12 ling\346\237\245\350\257\242.md" | 73 +++++++++++++++++++ 4 files changed, 108 insertions(+), 102 deletions(-) delete mode 100644 "\344\270\201\346\235\250\346\202\246/2024-11-14 mvc\345\212\240\345\274\272.md" delete mode 100644 "\344\270\201\346\235\250\346\202\246/2024-11-20 mvc\345\246\202\344\275\225\347\273\231\346\226\271\346\263\225\344\274\240\351\200\222\345\217\202\346\225\260.md" create mode 100644 "\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-09 \346\225\260\346\215\256\347\232\204\346\214\201\344\271\205\345\214\226.md" create mode 100644 "\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-12 ling\346\237\245\350\257\242.md" diff --git "a/\344\270\201\346\235\250\346\202\246/2024-11-14 mvc\345\212\240\345\274\272.md" "b/\344\270\201\346\235\250\346\202\246/2024-11-14 mvc\345\212\240\345\274\272.md" deleted file mode 100644 index 791a40e..0000000 --- "a/\344\270\201\346\235\250\346\202\246/2024-11-14 mvc\345\212\240\345\274\272.md" +++ /dev/null @@ -1,57 +0,0 @@ -## 创建ASP.NET MVC项目 -1. 打开 Visual Studio。 -2. 选择“文件” > “新建” > “项目”。 -3. 在“新建项目”对话框中,选择“Web” > “ASP.NET Web 应用程序”。 -4. 输入项目名称和位置。 -5. 选择 MVC 模板并点击“创建”。 - -## 路由 -ASP.NET MVC 使用路由来映射 URL 到控制器动作。路由配置通常在 `Global.asax` 文件中设置。 - -## 控制器 -控制器是处理用户请求的入口点。每个控制器类包含多个动作方法,每个方法对应一个特定的请求。 - -### 创建控制器 -```csharp -public class HomeController : Controller -{ - public ActionResult Index() - { - return View(); - } - - public ActionResult About() - { - return View(); - } -} -``` - -## 模型 -模型通常由 POCO(Plain Old CLR Objects)类表示,它们包含应用程序的数据和业务逻辑。 - -### 创建模型 -```csharp -public class Product -{ - public int Id { get; set; } - public string Name { get; set; } - public decimal Price { get; set; } -} -``` - -## 视图 -视图是 MVC 架构中的用户界面组件。在 ASP.NET MVC 中,视图通常使用 Razor 语法。 - -### 创建视图 -```html -@model IEnumerable - -@foreach (var item in Model) -{ -
-

@item.Name

-

@item.Price.ToString("C")

-
-} -``` \ No newline at end of file diff --git "a/\344\270\201\346\235\250\346\202\246/2024-11-20 mvc\345\246\202\344\275\225\347\273\231\346\226\271\346\263\225\344\274\240\351\200\222\345\217\202\346\225\260.md" "b/\344\270\201\346\235\250\346\202\246/2024-11-20 mvc\345\246\202\344\275\225\347\273\231\346\226\271\346\263\225\344\274\240\351\200\222\345\217\202\346\225\260.md" deleted file mode 100644 index 7d46b71..0000000 --- "a/\344\270\201\346\235\250\346\202\246/2024-11-20 mvc\345\246\202\344\275\225\347\273\231\346\226\271\346\263\225\344\274\240\351\200\222\345\217\202\346\225\260.md" +++ /dev/null @@ -1,45 +0,0 @@ -### 步骤(常规方法) -1. 新建一个控制器--`LarsController.cs` - ``` - using Microsoft.AspNetCore.Mvc; - namespace Blog.Controller; - public class LarsController:Controller -----继承 - { - public IActionResult Index() - { - return View(); - } - } - ``` -2. 获取id - ``` - // program.cs中默认值是id - - public IActionResult Index(int id) - { - return Content(id.ToString()); - } - ``` -3. 获取方法中的name - ``` - public class Students - { - public string name{get;set;}=null!; ----字符串需要=null! - public int age{get;set;} - } - - // 在LarsController中 - [HttpPost] - public IActionResult Create([FromBody]Students stu) - { - return Content(stu.name); - } - - //[FromBody]传递复杂数据 - ``` - - 作业: - ![](./图片/m1.png) - - ![](./图片/m2.png) - diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-09 \346\225\260\346\215\256\347\232\204\346\214\201\344\271\205\345\214\226.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-09 \346\225\260\346\215\256\347\232\204\346\214\201\344\271\205\345\214\226.md" new file mode 100644 index 0000000..c17eb65 --- /dev/null +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-09 \346\225\260\346\215\256\347\232\204\346\214\201\344\271\205\345\214\226.md" @@ -0,0 +1,35 @@ +# 数据的持久化 + +应用EntityFrameworkCore的步骤 +1. 安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +2. 定义数据库表模型 +```` +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!; +} +```` + +3. 定义数据库上下文 +```` +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; +public class BlogDbContext : DbContext{ + public DbSet Blogs {get;set;}=null!; + + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=.\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +```` + +4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +5. 将上一步生成的迁移文件,更新到数据库:dotnet ef database update(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) \ No newline at end of file diff --git "a/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-12 ling\346\237\245\350\257\242.md" "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-12 ling\346\237\245\350\257\242.md" new file mode 100644 index 0000000..94f9f38 --- /dev/null +++ "b/\344\270\201\346\235\250\346\202\246/\347\254\224\350\256\260/2024-12-12 ling\346\237\245\350\257\242.md" @@ -0,0 +1,73 @@ +检索元素的子集以生成新序列,而不修改各个元素。 然后,查询可能以各种方式对返回的序列进行排序或分组,如下面的示例所示(假定 scores 是 int[]): +``` +IEnumerable highScoresQuery = + from score in scores + where score > 80 + orderby score descending + select score; +``` +下面的代码示例演示一个简单查询表达式,它具有一个数据源、一个筛选子句、一个排序子句并且不转换源元素。 该查询以 select 子句结尾。 +``` +// Data source. +int[] scores = [90, 71, 82, 93, 75, 82]; + +// Query Expression. +IEnumerable scoreQuery = //query variable + from score in scores //required + where score > 80 // optional + orderby score descending // optional + select score; //must end with select or group + +// Execute the query to produce the results +foreach (var testScore in scoreQuery) +{ + Console.WriteLine(testScore); +} + +// Output: 93 90 82 82 +``` + + +# 作业 +## 中级练习 +16. 查询并选择特定属性 找出所有学生的姓名和年龄。 +var num = students.Select(x => new { x.Name, x.Age }); + +17. 查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 +var list = students.GroupBy(x => x.Age).Select(s => new { Age = s.Key, Count = s.Count() }); + +18. 查询并联结 联结学生和课程表,找出每个学生的所有课程。 +var list = students.Join(courses, s => s.Id, c => c.StudentId, + (students, courses) => new { studentName = students.Name, courses = courses.CourseName }); + +19. 查询并反转 反转数组中的元素顺序 +var list = numbers.Reverse(); + +20. 查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + +21. 查询并排除 从数组中排除所有小于5的元素。 +var list = numbers.Where(x => x > 5); + +22. 查询并填充默认值 如果数组中存在null值,用默认值0替换。 +var list = nullableNumbers.Select(n => n ?? 0); + +23. 查询并转换类型 将字符串数组转换为整数数组。 +var list = stringNumbers.Select(s => int.Parse(s)); + +24. 查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素 +var result = objects.OfType(); +## 高级练习 +25. 查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 +var list = numbers1.Zip(numbers2, (x, y) => x + " " + y); + +26. 查询并使用Range生成 生成一个包含1到10的整数数组。 +var list = Enumerable.Range(1, 10); + +27. 查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 +var list = Enumerable.Repeat("我是一个元素", 10); + +28. 查询并使用Take限制数量 从数组中取出前5个元素。 +var list = numbers.Take(5); + +29. 查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 +var list = numbers.Skip(3); \ No newline at end of file -- Gitee