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