日期:2014-05-17  浏览次数:20777 次

list<DateTime> 排序
list<DateTime>  li.OrderByDescending(a => a.Year).ToList();

li我想先按年倒序派,如果年相同再按月升序派,该怎么写?  都是一个字段



                magaView.HasMonth = db.Library.Where(a => a.CategoryID == categoryId).GroupBy(a => a.TimeBook.Year * 12 + a.TimeBook.Month - 1).Select(a => a.Key).ToList().     //找寻有杂志的月份
                    Select(a => new DateTime((int)Math.Floor(a / 12.0), a % 12 + 1, 1)).OrderByDescending(a => a.Year).ToList();

------解决方案--------------------
.OrderByDescending(a => a.Year).ThenByDescending(a=>a.Month).ToList();
------解决方案--------------------
 var query = li.OrderByDescending(a => a.Year).ThenBy(a => a.Month).ToList();

List<DateTime> li = new List<DateTime>() { DateTime.Now, DateTime.Now.AddMonths(1), DateTime.Now.AddMonths(1), DateTime.Now.AddMonths(2), DateTime.Now.AddMonths(2), DateTime.Now.AddYears(1), DateTime.Now.AddYears(1).AddMonths(2), DateTime.Now.AddYears(2).AddMonths(1), DateTime.Now.AddYears(2) };
            li.ForEach(x => Console.WriteLine(x));
            Console.WriteLine("--------------------------------");
            var query = li.OrderByDescending(a => a.Year).ThenBy(a => a.Month).ToList();
            query.ForEach(x => Console.WriteLine(x));

------解决方案--------------------

 List<DateTime> list = new List<DateTime>
            {
                DateTime.Now.AddYears(1),
                DateTime.Now.AddYears(1).AddMonths(1),
                DateTime.Now.AddYears(1).AddMonths(2),
                DateTime.Now.AddYears(1).AddMonths(3),
                DateTime.Now.AddYears(1).AddMonths(4),
                DateTime.Now.AddYears(1).AddMonths(5),
                DateTime.Now.AddYears(1).AddMonths(6),
                DateTime.Now.AddYears(2),
                DateTime.Now.AddYears(2).AddMonths(1),
                DateTime.Now.AddYears(2).AddMonths(1)