假设我在一个数据库表中有这些值
id = 1
StartDate = 1/3/2010
EndDate = 1/3/2010
id = 2
StartDate = 1/3/2010
EndDate = 1/9/2010
到目前为止,我的linq已经有了这个orderby。
var hold = MyList.OrderBy(x => x.StartDate).ToList();
我想订购它,但也使用结束日期。
就像我想要的顺序一样
id 2
id 1
因此,较大的endDates
优先使用。我不确定是否需要将其更改为使用某个比较函数或其他什么。
发布于 2010-01-02 05:23:06
MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);
发布于 2010-01-02 05:23:12
var hold = MyList.OrderBy(x => x.StartDate)
.ThenByDescending(x => x.EndDate)
.ToList();
您还可以使用查询语法,并说:
var hold = (from x in MyList
orderby x.StartDate, x.EndDate descending
select x).ToList();
ThenByDescending
是OrderBy
返回的IOrderedEnumerable
上的一个扩展方法。另请参阅相关方法ThenBy
。
发布于 2016-09-22 16:54:58
如果您有两个或更多字段要订购,请尝试以下操作:
var soterdList = initialList.OrderBy(x => x.Priority).
ThenBy(x => x.ArrivalDate).
ThenBy(x => x.ShipDate);
您可以使用类"ThenBy“添加其他字段
https://stackoverflow.com/questions/1989674
复制相似问题