linq 日期分组统计


 #region
        string Condition(DateTime date, string type)
        {
            if (date == null)
            {
                date = DateTime.MinValue;
            }

            if (type == "day")
            {
                return date.ToString("yyyy-M-d");
            }
            else if (type == "month")
            {
                return date.ToString("yyyy-M");
            }
            return date.Year.ToString();
        }
        public void xx()
        {
            List<DateTime> datelist = new List<DateTime>();
            var today = DateTime.Now.Date;
            for (var i = 0; i < 7;i++ )
            {
                datelist.Add(today.AddDays(-i));
            }

            var oderlist = this.Find().AsEnumerable();
            var result = from m in datelist
                         join n in oderlist on Condition(m, "day") equals Condition(n.create_time, "day") into c
                         from cc in c.DefaultIfEmpty()
                         group cc by Condition(m, "day") into g
                         select new { g.Key, Total = (g.FirstOrDefault()==null?0:g.Count())};

            var ccc = result.ToList();

        }
        #endregion


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM