linq 日期分组统计

时间:2022-10-13 14:01:52
 #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