C#中的Orderby子句
C#中使用orderby根据特定顺序按指定字段对集合中的元素进行排序。顺序可以是升序或降序。
以下是我们的元素列表-
List<string> myList = new List<string>();
//添加元素
myList.Add("iOS by Apple");
myList.Add("Android by Google");
myList.Add("Symbian by Nokia");现在,使用Orderby以降序对元素进行排序-
var myLen = from element in myList orderby element.Length descending select element;
以下是完整的代码-
示例
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<string> myList = new List<string>();
myList.Add("iOS by Apple");
myList.Add("Android by Google");
myList.Add("Symbian by Nokia");
var myLen = from element in myList
orderby element.Length descending
select element;
Console.WriteLine("Descending order...");
foreach (string str in myLen) {
Console.WriteLine(str);
}
}
}输出结果
Descending order... Android by Google Symbian by Nokia iOS by Apple
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短