diff --git "a/\346\233\276\345\273\272\344\270\234/20230522-\347\254\254\344\270\200\350\257\276.md" "b/\346\233\276\345\273\272\344\270\234/20230522-\347\254\254\344\270\200\350\257\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..5b3445f13bd309300ee3032ce25a3a691f8c543e --- /dev/null +++ "b/\346\233\276\345\273\272\344\270\234/20230522-\347\254\254\344\270\200\350\257\276.md" @@ -0,0 +1,65 @@ +## 创建项目 +``` +dotnet new console +``` + +## 题目1 +```c# +// 筛选偶数,并从大到小排序 +var lst = new List(){1,2,3,4,5,6,7,8,9,0}; +``` +## 答案 +```c# +//方式1 使用类sql查询语句进行查询 +IEnumerable result = +from e in lst +where e % 2 == 0 +orderby e descending +select e; + +Console.WriteLine(JsonSerializer.Serialize(result)); +``` +![样图](.\\imgs\\1684743458251.png) + +```c# +// 方式2 扩展写法 +IEnumerable result = +lst.where(e=>e%2==0) +.OrderByDescending(e=>e) +.select(e=>e); +``` + +## 题目2 + +## 答案1 +```c# +///统计随机数出现的次数。 +var rnd = new Random(1000); +var arr = Enumerable.Range(0,100).Select(_ => rnd.Next(0,10)); + +//定义计数数组 +int[] counts = new int[100]; +foreach(var e in arr){ + counts[e]++; +} + +for (int i = 0; i < counts.Length; i++) +{ + if(counts[i] != 0){ + System.Console.WriteLine($"字符{i}出现了--{counts[i]}--次"); + } +} +//输出结果 + +``` +## 答案2 +```c# +//通过linq语句将集合通过数值分组 +//通过select语句返回一个含有num和count属性的匿名类型 +let result = arr.GroupBy(e=>e).Select(e=> new {Num=e.Key,Count=e.Count()}); +//遍历输出 +foreach(var e in result){ + Console.WriteLine($"{e.Num}出现了:{e.Count}次"); +} + +``` diff --git "a/\346\233\276\345\273\272\344\270\234/imgs/1684743458251.png" "b/\346\233\276\345\273\272\344\270\234/imgs/1684743458251.png" new file mode 100644 index 0000000000000000000000000000000000000000..b19a6952250c93a7fce643cff3a7efa2daa2ac99 Binary files /dev/null and "b/\346\233\276\345\273\272\344\270\234/imgs/1684743458251.png" differ