浏览器标题切换
浏览器标题切换end

Intern Day10 - LINQ两个简单的例子

对字符串长度进行排序

using System;
using System.Linq;
using System.Resources;
using System.Runtime.InteropServices.ComTypes;

namespace Practice_Code
{
     public class Program
     {
         public static void Main(string[] args)
         {
             string[] languages = {"Java", "C", "C++", "Python", "C#"};
         
             var query = from i in languages
                 group i by i.Length
                 into lengthGroup // 按键值的长度进行排序并保存到lengthGroup中
                 orderby lengthGroup.Key // 协变(小string->大object)与逆变(大object->小string)
                 select lengthGroup;
         
             foreach(var i in query)
             {
                 Console.WriteLine("string of length:{0}",i.Key);
                 foreach (var str in i)
                 {
                     Console.WriteLine(str);
                 }
             }        
         }
    }
}

输出:

undefined

对成绩进行降序排序

using System;
using System.Linq;
using System.Resources;
using System.Runtime.InteropServices.ComTypes;

namespace Practice_Code
{
     public class Program
     {
         public static void Main(string[] args)
         {
             int[] scores = new int[] {86, 94, 100, 59, 79};
             // 或者 int[ ] x = new int[100];

             //IEnumberable<int> scoreQuery =
             var scoreQuery=  // var隐式转换 var是IEnumberable的语法糖
                 from score in scores
                 where score > 80
                 orderby score descending // 将输出结果进行降序排列
                 select score;

             foreach (var i in scoreQuery)
             {
                 Console.WriteLine(i);
             }    
         }
    }
}

输出:

undefined

参考

参考视频:https://www.bilibili.com/video/BV1xJ411g7Hm?p=10

posted @   抓水母的派大星  阅读(43)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2020-02-27 寒假Day35:HDU3499
2020-02-27 寒假Day35:HDU1385-Minimum Transport Cost -Floyd路径输出(输出字典序小的)
点击右上角即可分享
微信分享提示