Intern Day10

对字符串长度进行排序

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);
                 }
             }        
         }
    }
}

输出:

对成绩进行降序排序

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);
             }    
         }
    }
}

输出:

参考

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

原文地址:https://www.cnblogs.com/OFSHK/p/14456342.html