博客
关于我
C#中使用OrderBy和ThenBy等方法对List集合进行排序
阅读量:507 次
发布时间:2019-03-07

本文共 695 字,大约阅读时间需要 2 分钟。

  在C#的List操作中,针对List对象集合的排序我们可以使用OrderBy、OrderByDescending、ThenBy、ThenByDescending等方法按照特定的对象属性进行排序,其中Orderby表示按指定的第一个属性升序排序,OrderByDescending表示按指定的第一个属性降序排序,如果排序需要使用到不止一个条件的时候,可先使用OrderBy或者OrderByDescending方法排序完成后,再在方法链中调用ThenBy或者ThenByDescending对第二个条件排序,ThenBy会进行升序排序,ThenByDescending则是进行降序排序。

例如一个订单的Order类的定义如下:

1

2

3

4

5

6

7

8

public class Order

 {

       public string DepCode { setget; }

 

       public string DepName { setget; }

 

       public decimal Amount { setget; }

}

针对订单类的List集合orderList对象进行排序,排序规则为:先按科室编码DepCode升序排序,而后根据订单金额Amount进行降序排序。则相应的语句如下:

1

orderList = orderList.OrderBy(t => t.DepCode).ThenByDescending(t => t.Amount).ToList();

上述语句中t => t.DepCode的形式是Lambda表达式的写法,t代表orderList集合中的Order对象实体。

转载地址:http://tscjz.baihongyu.com/

你可能感兴趣的文章
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
Nginx、HAProxy、LVS
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
nginx中配置root和alias的区别
查看>>
nginx主要流程(未完成)
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx从入门到精通
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>