From 1a947af15883349e7322ba46c9c88879be70ee83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E7=BE=A4?= Date: Sun, 15 Dec 2024 13:10:42 +0800 Subject: [PATCH] =?UTF-8?q?20241211/13=E7=AC=94=E8=AE=B0+=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...03\344\271\240\350\256\262\350\247\243.md" | 125 ++++++++ ...30\347\272\247\347\273\203\344\271\240.md" | 297 ++++++++++++++++++ 2 files changed, 422 insertions(+) create mode 100644 "\350\222\213\347\276\244/20241211-Linq\347\273\203\344\271\240\350\256\262\350\247\243.md" create mode 100644 "\350\222\213\347\276\244/20241213-Linq\345\237\272\347\241\200+\344\270\255+\351\253\230\347\272\247\347\273\203\344\271\240.md" diff --git "a/\350\222\213\347\276\244/20241211-Linq\347\273\203\344\271\240\350\256\262\350\247\243.md" "b/\350\222\213\347\276\244/20241211-Linq\347\273\203\344\271\240\350\256\262\350\247\243.md" new file mode 100644 index 0000000..73e71e2 --- /dev/null +++ "b/\350\222\213\347\276\244/20241211-Linq\347\273\203\344\271\240\350\256\262\350\247\243.md" @@ -0,0 +1,125 @@ +### 基础练习 +Linq查询 +1. where +用于根据条件筛选集合中的元素。 +2.Select() +```css +// 查询并转换元素 将数组中的每个数字乘以2。 + + public dynamic T3(){ + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num3=numbers.Select(x=>x*2); + return num3; + } +``` +用于对集合中的每个元素进行转换或映射.列入 将整数列表中的每一个元素x2。 +3.StartsWith +```css +//4. 查询特定属性的对象 找出所有名字以"王"开头的学生。 +public class Student{ + public int Id{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} +} +public dynamic T4(){ + 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 num4=students.Where(students=>students.Name.StartsWith("王")); + return num4; +} +``` +用于筛选字符串字段以及特定前缀开始的记录。例如:用于筛选用户的名字。 + +4. OrderByDescending +用于对输入的序列中的元素进行降序排列 +例如 +```js +// 5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + +public dynamic T5(){ + 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 num5=students.Where(x=>x.Age>20).OrderByDescending(x=>x.Age).ToArray(); + return num5; +} +``` +5. Distinct +用于从序列中去除重复的元素,返回一个不包含重复地方元素 +```js +//6.查询并去重 找出数组中所有不重复的数字。 + +public dynamic T6(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num6=numbers.Distinct().ToArray(); + return num6; + +} + +``` +6. FirstOrDefault +用于返回序列中的第一个元素,如果序列为空,则返回默认值‌。 +```js +//7.查询第一个元素 找出数组中第一个大于3的元素。 +public dynamic T7(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num7=numbers.FirstOrDefault(x=>x>3).ToString(); + return num7; + +} +``` +7.Any +‌LINQ中的Any方法用于检查集合中是否存在满足指定条件的元素,并返回一个布尔值‌ +```js +public dynamic T9(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num9=numbers.Any(x=>x>10).ToString(); + return num9; + +} +``` +8.Count +用于计算总数量 +```js +public dynamic T10(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num10=numbers.Count(x=>x>5).ToString(); + return num10; + +} +``` +9. Sum +计算总和 +```css +public dynamic T11(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num11=numbers.Sum(); + return num11; + +} +``` +10.Min +最小值 + +11. Max() +最大值 +12. Average()平均值 + +13. Reverse() +反转数组 + +14. First() +第一个元素 + + diff --git "a/\350\222\213\347\276\244/20241213-Linq\345\237\272\347\241\200+\344\270\255+\351\253\230\347\272\247\347\273\203\344\271\240.md" "b/\350\222\213\347\276\244/20241213-Linq\345\237\272\347\241\200+\344\270\255+\351\253\230\347\272\247\347\273\203\344\271\240.md" new file mode 100644 index 0000000..e762022 --- /dev/null +++ "b/\350\222\213\347\276\244/20241213-Linq\345\237\272\347\241\200+\344\270\255+\351\253\230\347\272\247\347\273\203\344\271\240.md" @@ -0,0 +1,297 @@ +### 基础作业 +核心代码: +```js +using Microsoft.AspNetCore.Mvc; +using Blog.Models; + +namespace Blog.Controllers; + +public class LinqController:Controller{ + //1.查询特定元素 找出数组中等于5的元素。 + public dynamic T1(){ + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + + var num=numbers.Where(x=>x==5).ToArray(); + return num; + } + + //2.查询特定范围的元素 找出数组中在2到8之间的元素。 + public dynamic T2(){ + + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num2=numbers.Where(x=>x>=2&&x<=8); + return num2; + + } + // 查询并转换元素 将数组中的每个数字乘以2。 + + public dynamic T3(){ + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num3=numbers.Select(x=>x*2); + return num3; + } +//4. 查询特定属性的对象 找出所有名字以"王"开头的学生。 +public class Student{ + public int Id{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} +} +public dynamic T4(){ + 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 num4=students.Where(students=>students.Name.StartsWith("王")); + return num4; +} +// 5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + +public dynamic T5(){ + 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 num5=students.Where(x=>x.Age>20).OrderByDescending(x=>x.Age).ToArray(); + return num5; +} + +//6.查询并去重 找出数组中所有不重复的数字。 + +public dynamic T6(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num6=numbers.Distinct().ToArray(); + return num6; + +} +//7.查询第一个元素 找出数组中第一个大于3的元素。 +public dynamic T7(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num7=numbers.FirstOrDefault(x=>x>3).ToString(); + return num7; + +} +//8.查询最后一个元素 找出数组中最后一个小于7的元素。 +public dynamic T8(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num8=numbers.Last().ToString(); + return num8; +} +//9.查询元素是否存在 检查数组中是否存在大于10的元素。 +public dynamic T9(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num9=numbers.Any(x=>x>10); + return num9; + +} +//10.查询元素的计数 计算数组中大于5的元素数量。 +public dynamic T10(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num10=numbers.Count(x=>x>5).ToString(); + return num10; + +} +//11.查询元素的总和 计算数组中所有元素的总和。 +public dynamic T11(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num11=numbers.Sum(); + return num11; + +} +//12.查询元素的最大值 找出数组中的最大值 +public dynamic T12(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num12=numbers.Max().ToString(); + return num12; + +} +//13.查询元素的最小值 找出数组中的最小值。 +public dynamic T13(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num13=numbers.Min().ToString(); + return num13; + +} +//14.查询元素的平均值 计算数组中所有元素的平均值。 +public dynamic T14(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num14=numbers.Average().ToString(); + return num14; + +} +//15.查询特定条件的元素 找出数组中能被3整除的元素。 +public dynamic T15(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num15=numbers.Where(x=>x%3==0).ToArray(); + return num15; +} + +} +``` +### 中/高级作业 +```js +using Microsoft.AspNetCore.Mvc; +using Blog.Models; +using Microsoft.VisualBasic; +namespace Blog.Controllers; +public class ZhongController : Controller{ + +public class Student{ +public int Id{get;set;} +public string Name{get;set;}=null!; +public int Age{get;set;} +} + +//16.查询并选择特定属性 找出所有学生的姓名和年龄。 + public dynamic Index(){ + 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 num16=students.Select(x=>new {x.Name,x.Age}).ToList(); + return num16; + } + //17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量 + public dynamic t17(){ + 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 num17=students.GroupBy(s=>s.Age).Select(s=>new{s.Key,Count=s.Count()}); + return num17; + } + + public class Course{ + public int StudentId{get;set;} + public string CourseName{get;set;}=null!; + } +//18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 +public dynamic T18(){ + + 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 num18=students.SelectMany(x=>courses.Where(c=>c.StudentId==x.Id).Select(c=>new{x.Name,c.CourseName})).ToList(); + return num18; + +} +//19.查询并反转 反转数组中的元素顺序 + +public dynamic T19(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num19=numbers.Reverse().ToArray(); + return num19; + +} +//20.查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置 + public dynamic T20(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num20=numbers.Select(s=>s>2?s:numbers.First(x=>x>2)).ToArray(); + return num20; + + } + //21.查询并排除 从数组中排除所有小于5的元素。 + +public dynamic T21(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num20=numbers.Where(x=>x>=5).ToArray(); + return num20; + + } + //22.查询并填充默认值 如果数组中存在null值,用默认值0替换。 + public dynamic T22(){ + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + var num22=nullableNumbers.Select(x=>x??0).ToArray(); + return num22; + + } + //23.查询并转换类型 将字符串数组转换为整数数组。 + public dynamic T23(){ + string[] stringNumbers = { "1", "2", "3", "4" }; + var num23=stringNumbers.Select(int.Parse).ToArray(); + return num23; + + } + //24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + public dynamic T24(){ + object[] objects = { "String", 123, "Another String", 456 }; + var num24 = objects.OfType().ToList(); + return num24; + + } + //25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组 + + public dynamic T25(){ + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + var num25=numbers1.Zip(numbers2,(first,second)=>(first,second)).ToArray(); + var n=numbers1.Zip(numbers2,(first,second)=>new { first, second }).ToArray(); + return n; + } + + //26.查询并使用Range生成 生成一个包含1到10的整数数组。 + public dynamic T26(){ + int[] result = Enumerable.Range(1,10).ToArray(); + return result; + } + //27.查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + public dynamic T27(){ + int a = 6; + int[] result = Enumerable.Repeat(a,10).ToArray(); + return result; + } + //28.查询并使用Take限制数量 从数组中取出前5个元素。 + public dynamic T28(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num28=numbers.Take(5).ToArray(); + return 28; + + } + //29.查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + public dynamic T29(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num29=numbers.Skip(3).ToArray(); + return 29; + } + +} +``` \ No newline at end of file -- Gitee