首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

list<DateTime> 排序,该如何处理

2013-04-05 
listDateTime排序listDateTimeli.OrderByDescending(a a.Year).ToList()li我想先按年倒序派,如果

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),


                DateTime.Now.AddYears(2).AddMonths(1),
                DateTime.Now.AddYears(2).AddMonths(1),
                DateTime.Now.AddYears(2).AddMonths(1),
                DateTime.Now.AddYears(2).AddMonths(1),
                DateTime.Now.AddYears(3),
                DateTime.Now.AddYears(4),
                DateTime.Now.AddYears(5),
                DateTime.Now.AddYears(6),
                DateTime.Now.AddYears(7),
                DateTime.Now.AddYears(8),
                DateTime.Now.AddYears(9),
            };
            var query_list = list.OrderByDescending(x => x.Year).OrderBy(x => x.Month);
            foreach (var item in query_list)
            {
                Console.WriteLine(item.ToString());
            }
            Console.Read();

热点排行