ef根據年月進行分組

 /// <summary>
        /// 根據年月進行分組 數據庫日期格式爲 yyyy-MM-dd HH:mm:ss
        /// </summary>
        public static void YearMonth()
        {
            using (ZDPORTALEntities db=new ZDPORTALEntities())
            {
                //Expression<Func<News,object>> w=t=>new{Year=t.N_ReleaseDate.Value.Year,Month=t.N_ReleaseDate.Value.Month};
                var q = db.News.GroupBy(t=>new{Year=t.N_ReleaseDate.Value.Year,Month=t.N_ReleaseDate.Value.Month});
                var result = q.Select(t => new { Keys = t.Key.Year + "-" + (t.Key.Month.ToString().Length >= 2 ? t.Key.Month.ToString() : "0" + t.Key.Month.ToString())});
                foreach (var item in result)
                {
                    Console.WriteLine(item.Keys);
                }
            }
        }
    }

運行結果如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章