如何使用排序依据与LINQ 2场? [英] How to use orderby with 2 fields in linq?

查看:109
本文介绍了如何使用排序依据与LINQ 2场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个数据库表中这些值

  ID = 1 
起始日期= 2010年1月3日
结束日期= 2010年1月3日

ID = 2
起始日期= 2010年1月3日
结束日期= 1/9/2010

现在我迄今这个排序依据我的LINQ



VAR保持= MyList.OrderBy(X => x.StartDate).ToList();



我想不过来订购吧也使用结束日期

$ b。
$ b

像这样的顺序我就在想这是

  ID 2 
ID 1

所以endDates是更大的先走。我不知道我是否需要更改此使用一些比较功能什么的。



感谢


解决方案

  MyList.OrderBy(X => x.StartDate).ThenByDescending(X => x.EndDate); 


Say I have these values in a database table

id = 1
StartDate = 1/3/2010
EndDate =  1/3/2010

id = 2
StartDate = 1/3/2010
EndDate = 1/9/2010

Now I have so far this orderby for my linq

var hold = MyList.OrderBy(x => x.StartDate).ToList();

I want to order it however also using the end date.

Like so the order I would want this in as

id 2
id 1

So endDates that are greater go first. I am not sure if I need to change this to use some compare function or something.

Thanks

解决方案

MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);

这篇关于如何使用排序依据与LINQ 2场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆