对数据排序 (C#) |
您所在的位置:网站首页 › 对表格数据排序不能按照顺序排列 › 对数据排序 (C#) |
对数据排序 (C#)
项目
04/07/2023
排序操作基于一个或多个属性对序列的元素进行排序。 第一个排序条件对元素执行主要排序。 通过指定第二个排序条件,您可以对每个主要排序组内的元素进行排序。 下图展示了对一系列字符执行按字母顺序排序操作的结果。 下节列出了对数据进行排序的标准查询运算符方法。 方法 方法名 描述 C# 查询表达式语法 更多信息 OrderBy 按升序对值排序。 orderby Enumerable.OrderByQueryable.OrderBy OrderByDescending 按降序对值排序。 orderby … descending Enumerable.OrderByDescendingQueryable.OrderByDescending ThenBy 按升序执行次要排序。 orderby …, … Enumerable.ThenByQueryable.ThenBy ThenByDescending 按降序执行次要排序。 orderby …, … descending Enumerable.ThenByDescendingQueryable.ThenByDescending Reverse 反转集合中元素的顺序。 不适用。 Enumerable.ReverseQueryable.Reverse 查询表达式语法示例 主要排序示例 主要升序排序下面的示例演示如何在 LINQ 查询中使用 orderby 子句按字符串长度对数组中的字符串进行升序排序。 string[] words = { "the", "quick", "brown", "fox", "jumps" }; IEnumerable query = from word in words orderby word.Length select word; foreach (string str in query) Console.WriteLine(str); /* This code produces the following output: the fox quick brown jumps */ 主要降序排序下面的示例演示如何在 LINQ 查询中使用 orderby descending 子句按字符串的第一个字母对字符串进行降序排序。 string[] words = { "the", "quick", "brown", "fox", "jumps" }; IEnumerable query = from word in words orderby word.Substring(0, 1) descending select word; foreach (string str in query) Console.WriteLine(str); /* This code produces the following output: the quick jumps fox brown */ 次要排序示例 次要升序排序下面的示例演示如何在 LINQ 查询中使用 orderby 子句对数组中的字符串执行主要和次要排序。 首先按字符串长度,其次按字符串的第一个字母,对字符串进行升序排序。 string[] words = { "the", "quick", "brown", "fox", "jumps" }; IEnumerable query = from word in words orderby word.Length, word.Substring(0, 1) select word; foreach (string str in query) Console.WriteLine(str); /* This code produces the following output: fox the brown jumps quick */ 次要降序排序下面的示例演示如何在 LINQ 查询中使用 orderby descending 子句按升序执行主要排序,按降序执行次要排序。 首先按字符串长度,其次按字符串的第一个字母,对字符串进行排序。 string[] words = { "the", "quick", "brown", "fox", "jumps" }; IEnumerable query = from word in words orderby word.Length, word.Substring(0, 1) descending select word; foreach (string str in query) Console.WriteLine(str); /* This code produces the following output: the fox quick jumps brown */ 请参阅 System.Linq 标准查询运算符概述 (C#) orderby 子句 对 Join 子句的结果进行排序 如何按任意词或字段对文本数据进行排序或筛选 (LINQ) (C#) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |