diff --git "a/\345\215\242\346\200\235\346\207\277/20241114_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241114_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..42015d13b82cd47b9d8a81fd9b48616ada6ae51e --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241114_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" @@ -0,0 +1,15 @@ +# 笔记 +### 学会使用vscode终端命令写sln解决方案,mvc项目模型组 +#### 命令有: +> 1、dotnet new mvc -o 名 + 2、dotnet new sln -o 名 + 3、mkdir src + 4、dotnet sln add 项目路径 + 5、dotnet run + 6、结束终端任务快捷键:ctrl+c + 7、按tab键切换目录 + 8、dotnet watch + 9、dotnet build +# 作业 +**效果:** +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241117150337.png) \ No newline at end of file diff --git "a/\345\215\242\346\200\235\346\207\277/20241115_\350\257\276\345\240\202\347\254\224\350\256\260.md" "b/\345\215\242\346\200\235\346\207\277/20241115_\350\257\276\345\240\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..c759e59becf6389fac11be4774c3b7a752f1e625 --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241115_\350\257\276\345\240\202\347\254\224\350\256\260.md" @@ -0,0 +1,36 @@ +# 笔记 +``` +1. :建一个文件夹:mkdir Blog +2. :在创建的文件夹下(按tab建切换)新建sln文件:dotnet new sln -o .\Blog\ +3. :在创建的文件夹下新建src文件:mkdir .\Blog\src +4. :在src文件下新建一个mvc文件:dotnet new mvc -o .\Blog\src\Blog.Web + (该意思是在src目录下新建一个名叫Blog.Web的mvc文件) +5. :将mvc项目文件添加到sln解决方案当中:dotnet sln .\Blog\ add .\Blog\src\Blog.Web + (该意思是先找到sln所在目录,然后后面的文件被添加到sln里) +``` +******************** +``` +1. : dotnet new mvc -o Blog +2. : 在Home→index.cshtml里添加: + +3. :在Controllers里添加:BlogController.cs +4. :在BlogsController.cs里填写: +using Microsoft.AspNetCore.Mvc; //引入命名空间之前,要引用对应包或者库 + namespace Blog.Controllers; + public class BlogsController:Controller{ + public IActionResult Index() + { + return View(); + } + + } +5. :在Views里添加文件夹Blogs +6. :在Blogs里添加文件index.cshtml,里面可以写内容了 +7. :切换到Blog路径下:cd .\Blog\ +8. :打包:dotnet publish +9. :上传:scp -r .\bin\Release\net8.0\publish\* root@soft.com:/var/www/t5.soft.com + (*代表打包所有,"soft.com"是例子,要填写自己的服务器域名) +10.:部署:下载tabby和.NET 8.0 +``` +**端口号的数值区间是从0到65535** + diff --git "a/\345\215\242\346\200\235\346\207\277/20241119_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241119_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..68969434c5b06888e0b1b234a0b7d57df1d7544e --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241119_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" @@ -0,0 +1,136 @@ +# 笔记 +[HttpBody] +public lactionResult 方法名([fromBody]类){retrun Content(类.参数名)} +# 作业 +## Linux练习 +### 任务1 +**SSH客户端:** 网络协议客户端,免费(软件本身),命令行操作,基础SSH功能,命令行界面,依赖具体实现和配置,需要自定义配置和脚本的用户 +**Tabby应用:** 跨平台终端工具,免费(软件本身),高度可定制,支持多种协议和自定义配置,图形化界面,界面简洁优美,提供基本安全功能,可自定义安全设置,需要跨平台支持、高度可定制性和图形化界面的用户 +**Xshell:** SSH客户端软件,商业软件(需购买许可),功能强大,提供会话管理、脚本录制等高级功能, 图形化界面,功能布局清晰,提供基本安全功能,可能需要额外配置以满足高安全性需求,专业用户和企业环境,需要处理大量会话和高级功能的用户 +**Putty:** SSH客户端软件,免费,功能相对较少,但易于上手,图形化界面,界面简洁直观,提供基本加密和认证机制,初学者和只需要基本SSH功能的用户 +### 任务2 +apt update命令在Linux系统中扮演着至关重要的角色,它帮助确保你的系统能够获取到最新的软件包信息,并维护系统的安全性和稳定性。通过定期执行这个命令,你可以保持你的系统始终处于健康状态,并适应软件源的变化。因此,在进行任何软件管理操作之前,建议先运行apt update命令来更新软件包列表。 +### 任务3 +apt upgrade -y命令在维护Linux系统的安全性和稳定性方面发挥着重要作用,是系统管理员和日常用户应该定期执行的任务之一 +### 任务4 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-linux.gif) +### 任务5 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124162354.png) +## MVC练习1-12 +``` +1.命令:dotnet new console ;会生成一个.scproj和program.cs文件,.csproj项目名称和文件夹名有关 +2,3.命令:dotnet new console -n Blog -o Blog +4,5.命令:dotnet new mvc -n Mymvc -o Vlog +6.命令: 1-dotnet new sln -n Mysln + 2-dotnet new mvc -n LSYmvc -o LSY + 3-dotnet sln MySln.sln add LSY/LSYmvc.csproj + 4-dotnet new classlib -n Clb1 -o Clb1 + -dotnet sln .\Mysln.sln add .\Clb1\Clb1.csproj + 5-dotnet new classlib -n Clb2 -o Clb2 + -dotnet sln .\Mysln.sln add .\Clb2\Clb2.csproj + 6-dotnet new classlib -n Clb3 -o Clb3 + -dotnet sln .\Mysln.sln add .\Clb3\Clb3.csproj +--------- +7.代码:在homecontroller.cs填写 public IActionResult Ok() + { + return View(); + } + 在views中打开home,添加一个ok.cshtml里面填写内容 + 使用dotnet run,打开地址,http://localhost:5253/home/ok +--------- +8.代码:在controllers中打开添加一个文件名为Blogs.cs,填写 + using Microsoft.AspNetCore.Mvc; + namespace Blog.Controllers; + public class Blogs:Controller{ + public IActionResult Index(){ + return View(); + } + } +打开views并添加一个文件夹名为Blogs,在Blogs里添加一个Index.cshtml文件,并填写 神级预判 +---------- +9.代码:打开blog.cs在里面填写 + public IActionResult Music(){ + return View();} +打开views打开blogs添加一个Music.cshtml,填写 顶级打野 +----------- +10.代码:打开blog.cs在里面填写 + public IActionResult List(){ + return View();} +打开views打开blogs并添加一个List.cshtml,填写 +@* 增删改查页面 *@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
序号班级姓名年龄性别编辑
123级5班卢思懿20
223级5班郭梦媛20
323级5班陈凯杰19
+------------ +11.代码:打开controllers添加Products.cs,打开填写 + using Microsoft.AspNetCore.Mvc; + namespace Blog.Controllers; + public class Products:Controller{ + public IActionResult Edit(int id){ + return Content(id.ToString()); + } + } +//打开Views添加一个Products文件夹,在里面添加一个Edit.cshtml文件 +-------------- +12.代码:打开Products.cs,填写 + public IActionResult Create(Students student){ + student.Name = "卢思懿"; + return Content($"学生姓名:{student.Name}"); + } +添加一个学生类 public class Students + { + public string Name { get; set;} = null!; + public int Age { get; set; } + public double Height { get; set;} + } +``` +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/65f401ada2e57100687fd5a7779a922.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/b2af349b67e11466d8f7e9affbf02d4.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/683ded2fd2ed98dc7e734cfe9c4e15c.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/5a2e3351aa7e7fa0d23afdfb18fc9e5.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/de84cf8f69bb0ee47c41e06a2f4597b.png) \ No newline at end of file diff --git "a/\345\215\242\346\200\235\346\207\277/20241121_\350\257\276\345\240\202\347\254\224\350\256\260.md" "b/\345\215\242\346\200\235\346\207\277/20241121_\350\257\276\345\240\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..26150dbefb9e6dbaff10b02d91eb84311f2bf2f9 --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241121_\350\257\276\345\240\202\347\254\224\350\256\260.md" @@ -0,0 +1,21 @@ +# 笔记 +``` + public IActionResult Index(int id) + { return Content(id.ToString()); + } +dotnet run----http://localhost:xxxx/blog/index/?id=值 + +在program.cs项目中的 pattern: "{controller=Home}/{action=Index}/{id?}"; +将"id"修改为其它名,在方法里才能接受其他名的参数 + +public IActionResult Create(){ +var 对象名 = new 类名{ +name=“”, +author=“”, +···· +}; +return View(对象名) +} +然后在create.cshtml写@model Blog.Controllers.类名(BlogCreateDto); +@Model.对象(name/height/age) +``` \ No newline at end of file diff --git "a/\345\215\242\346\200\235\346\207\277/20241122_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241122_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b13d7eee8c5a79bd93bc118bdd6df7e49b8b7e63 --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241122_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" @@ -0,0 +1,37 @@ +# 笔记 +### 序列化 +``` +在ASP.NET Core中,序列化是将对象转换为JSON或XML格式的过程,以便可以轻松地通过HTTP传输或存储在文本文件中。 +反序列化是将JSON或XML格式的数据转换回对象的过程。 +ASP.NET Core通常使用内置的System.Text.Json库或流行的Newtonsoft.Json库来处理序列化和反序列化 +``` +### 重定向 return RedirectToAction() +### 视图 +在同一个控制器下的方法,可以打开的是1页面但是显示的是2.cshtml内容: +public IActionResult 1(){ +return View("2"); +} + +# 作业 +### 专项练习-控制器传参 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124193923.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124194017.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124194053.png) +### 专项练习-基础能力 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124211916.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-8-1.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-8-2.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212050.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-8-3.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212149.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-8-4.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212252.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-8-5.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212343.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1124-8-6.gif) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212430.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212508.png) +### 专项练习-控制器返回值 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212907.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/de84cf8f69bb0ee47c41e06a2f4597b.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241124212508.png) diff --git "a/\345\215\242\346\200\235\346\207\277/20241126_\350\257\276\345\240\202\347\254\224\350\256\260.md" "b/\345\215\242\346\200\235\346\207\277/20241126_\350\257\276\345\240\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..7e7977f85f2397b632c14817c363dd9aa191840b --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241126_\350\257\276\345\240\202\347\254\224\350\256\260.md" @@ -0,0 +1,23 @@ +# 笔记 +## 正向代理 +#### 定义:正向代理是指代表客户端向服务器发起请求的代理服务器。在这个过程中,正向代理隐藏了客户端的真实IP地址,将请求转发给目标服务器,然后将服务器的响应返回给客户端。 +#### 工作原理: +假设你是一个用户,你访问不了某个网站,但你能访问一个代理服务器。这个代理服务器能访问你无法访问的网站。于是你先连接上代理服务器,告诉它你需要那个无法访问网站的内容。代理服务器去取回来,然后返回给你。从网站的角度,只在代理服务器来取内容的时候有一次记录。 +#### 用途: +访问受限资源:正向代理可以绕过网络访问限制,访问那些被限制的资源,提高网络的访问自由度。 +保护隐私:隐藏客户端的真实IP地址,提高客户端的匿名性和隐私保护,防止客户端受到攻击。 +缓存资源:缓存常用的资源,减轻服务器的负载,提高网络的响应速度。 +## 反向代理 +#### 定义:反向代理是指用户不需要知道目标服务器的地址,也不需要在用户端做任何设定,就可以直接通过访问反向代理服务器来获得目标服务器的资源。在这个过程中,反向代理服务器位于用户与目标服务器之间,但对于用户而言,反向代理服务器就相当于目标服务器。 +#### 工作原理: +例如用户访问一个名为“ooxx”的网站的“readme”页面,但“ooxx”上并不存在这个页面,它是从另外一台服务器上取回来,然后作为自己的内容呈现给用户。用户并不知情,这很正常。这里所提到的“ooxx”这个域名对应的服务器就设置了反向代理功能。 +#### 用途: +提高安全性:反向代理对外都是透明的,访问者并不知道自己访问的是一个代理,从而隐藏了后端服务器的真实IP地址,提高了内部服务器的安全性。 +负载均衡:反向代理可以将请求分发到多个后端服务器,以平衡服务器的负载,提高系统的性能和可靠性。 +缓存:反向代理可以缓存静态内容,减轻后端服务器的负载,并提供更快的响应时间。 +启用高级URL策略:反向代理还可以启用高级URL策略和管理技术,从而使处于不同web服务器系统的web页面同时存在于同一个URL空间下。 +## 两者区别 +**代理对象:** 正向代理代表客户端发送请求,而反向代理代表服务器接收请求。 +**网络配置:** 正向代理通常位于客户端所在的网络中,而反向代理通常位于服务器所在的网络中。 +**请求方向:** 正向代理的请求流向是从客户端到代理服务器再到目标服务器,而反向代理的请求流向是从客户端到代理服务器再到后端服务器。 +**应用场景:** 正向代理常用于绕过网络限制、保护客户端隐私等,而反向代理常用于负载均衡、缓存、安全性和内容过滤等。 diff --git "a/\345\215\242\346\200\235\346\207\277/20241128_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241128_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..a1fbc3be92158a71369c5f8b5c702f7cb5906f03 --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241128_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" @@ -0,0 +1,26 @@ +# 笔记 +### 定位点 +```html + +``` +# 作业 +## 专业项练习-视图及其模板引擎 +### 1 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201164022.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201164032.png) +### 2 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201162608.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201162631.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201162643.png) +### 3 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201163614.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201163629.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201163644.png) +### 4 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201172632.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201172719.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201172740.png) +### 5 经典CRUD界面 +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201172901.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201175856.png) +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241201175748.png) \ No newline at end of file diff --git "a/\345\215\242\346\200\235\346\207\277/20241203_\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241203_\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..7dc02ed40bbfead2eea4124af655d4fede9a1b1b --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241203_\344\275\234\344\270\232.md" @@ -0,0 +1,3 @@ +# 作业 + +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1204.gif) \ No newline at end of file diff --git "a/\345\215\242\346\200\235\346\207\277/20241205_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241205_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..abbb35415dc3cef411c6093621859d7a5505db32 --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241205_\350\257\276\345\240\202\347\254\224\350\256\260+\344\275\234\344\270\232.md" @@ -0,0 +1,22 @@ +# 笔记 +### Linq集成查询和Lambda表达式 +#### 1.First() FirstOrDefault() 获取集合中(符合条件的)第一个 + +First() 这个如果没有获取到,则报错 + +FirstOrDefault() 没有获取到,则返回Null +#### 2.Single() SingleOrDefault() 获取集合中(符合条件)的其中一个 + +Single() 这个如果没有获取到,则报错 + +SingleOrDefault() 没有获取到,则返回Null +#### 3.Where() 获取集合中符合条件的元素,将它们筛选出来放入一个新的集合中返回 + +Where 查找符合条件的内容 +#### 4.Select() 返回指定内容 + +Select() 返回指定内容 Select(x=>new {x.Id}) + +# 作业 +**完成新增和删除** +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/1208.gif) diff --git "a/\345\215\242\346\200\235\346\207\277/20241206_\344\275\234\344\270\232.md" "b/\345\215\242\346\200\235\346\207\277/20241206_\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..9c8ccdd1fad3dde3a2ea9dc042f4b05499d8e94d --- /dev/null +++ "b/\345\215\242\346\200\235\346\207\277/20241206_\344\275\234\344\270\232.md" @@ -0,0 +1,219 @@ +# 作业29题 +### 1.查询特定元素 找出数组中等于5的元素 +```c# + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var result = numbers.Where(number => number == 5); +``` +### 2.查询特定范围的元素 找出数组中在2到8之间的元素 +```c# + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var result = numbers.Where(item => item > 2 && item < 8); +``` +### 3.查询并转换元素 将数组中的每个数字乘以2 +```c# + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var result = numbers.Select(item => item * = 2); +``` +### 4.查询特定属性的对象 找出所有名字以"王"开头的学生 +```c# +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 result = students.Where(item => item.Name.StartsWith('王')); +``` +### 5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列 +```c# + 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 }, +}; +var result = students.Where(item => item.Age>20).OrderByDescending(item => item.Age); +``` +### 6.查询并去重 找出数组中所有不重复的数字 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Distinct(); +``` +### 7.查询第一个元素 找出数组中第一个大于3的元素 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.FirstOrDefault(item => item > 3); +``` +### 8.查询最后一个元素 找出数组中最后一个小于7的元素 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.LastOrDefault(item => item > 7); +``` +### 9.查询元素是否存在 检查数组中是否存在大于10的元素 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Any(item => item > 10); +``` +### 10.查询元素的计数 计算数组中大于5的元素数量 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Where(item => item > 5).Count(); +``` +### 11.查询元素的总和 计算数组中所有元素的总和 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Select(item => item).Sum(); +``` +### 12.查询元素的最大值 找出数组中的最大值 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Max(); +``` +### 13.查询元素的最小值 找出数组中的最小值 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Min(); +``` +### 14.查询元素的平均值 计算数组中所有元素的平均值 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Average(); +``` +### 15.查询特定条件的元素 找出数组中能被3整除的元素 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Where(item => item % 3 == 0); +``` +### 16.查询并选择特定属性 找出所有学生的姓名和年龄 +```c# +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 }, +}; +var result = students.Select(item => new {Age = item.Age,Name = item.Name}); +``` +### 17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量 +```c# +var result = students.GroupBy(item => item.Age).Select(item => new {Age=item.Key,AgeCount=item.Count()}); +``` +### 18.查询并联结 联结学生和课程表,找出每个学生的所有课程 +```c# +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="语文"}, +}; +var result = from Student in students join Course in courses on Student.Id equals Course.StudentId select new StudentCourse { + StudentName=Student.Name, + CourseName=Course.CourseName + }; +``` +### 19.查询并反转 反转数组中的元素顺序 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Reverse(); +``` +### 20.查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置 +```c# +var Index=0; +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +for(int i=0;i2){ + Index=i; + break; + } +} +for(int i=Index+1;iitem>2); +} +``` +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241208164740.png) +### 21.查询并排除 从数组中排除所有小于5的元素 +```c# +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +var result = numbers.Where(item= > item>5); +``` +### 22.查询并填充默认值 如果数组中存在null值,用默认值0替换 +```c# +int?[] nullableNumbers = { 1, null, 3, null, 5 }; +for (int i = 0; i < nullableNumbers.Length; i++){ + if(nullableNumbers[i]==null){ + nullableNumbers[i]=0; + } +} +``` +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241208170120.png) +### 23.查询并转换类型 将字符串数组转换为整数数组 +```c# +string[] stringNumbers = { "1", "2", "3", "4" }; +var result = stringNumbers.Select(item => int.Parse(item)); +``` +### 24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素 +```c# +object[] objects = { "String", 123, "Another String", 456 }; +var result = objects.OfType().ToList(); +``` +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241208170630.png) +### 25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组 +```c# +int[] numbers1 = { 1, 2, 3 }; +int[] numbers2 = { 4, 5, 6 }; +var result = numbers1.Zip(numbers2); +``` +![](https://gitee.com/lu-siyi-641/gitee-image-warehouse/raw/master/photos/20241208170853.png) +### 26.查询并使用Range生成 生成一个包含1到10的整数数组 +```c# +int[] arr=new int[10]; +for (int i = 0; i