linq 排序

linq 排序

一、linq 排序用到的方法

方法名 说明 C# 查询表达式语法
OrderBy 按升序对值进行排序。 orderby
OrderByDescending 按降序对值进行排序。 orderby … descending
ThenBy 按升序执行次要排序。 orderby …, …
ThenByDescending 按降序执行次要排序。 orderby …, … descending

二、linq 排序实例

1、 主要升序排序

  • 
      string[] words = { "the", "quick", "brown", "fox", "jumps" };
      IEnumerable<string> query = from word in words orderby word.Length select word;
      foreach (string str in query) Console.WriteLine(str);
    
    		
  • 2、 主要降序排序

  • 
     string[] words = { "the", "quick", "brown", "fox", "jumps" };
     IEnumerable<string> query = from word in words orderby word.Substring(0, 1) descending select word;
     foreach (string str in query) Console.WriteLine(str);
    
    		
  • 3、次要升序排序

  • 
    string[] words = { "the", "quick", "brown", "fox", "jumps" };
    IEnumerable<string> query = from word in words orderby word.Length, word.Substring(0, 1) select word;
    foreach (string str in query) Console.WriteLine(str);
    
    		
  • 4、次要降序排序

  • 
      string[] words = { "the", "quick", "brown", "fox", "jumps" };
      IEnumerable<string> query = from word in words orderby word.Length, word.Substring(0, 1) descending select word;
      foreach (string str in query) Console.WriteLine(str);
    
    		
  • 标签: