linq带条件分组查询.

  折腾了1个小时,终于把这个问题解决了,分享下.哈哈.......

 先是获取上周的日期:

  string str = DateTime.Today.DayOfWeek.ToString().ToLower();

            DateTime endWeek = new DateTime();
            DateTime startWeek = new DateTime();

            switch (str)
            {
                case "monday":
                    endWeek = DateTime.Now.Date.AddDays(-1);
                    break;
                case "tuesday":
                    endWeek = DateTime.Now.Date.AddDays(-2);
                    break;
                case "wendesday":
                    endWeek = DateTime.Now.Date.AddDays(-3);
                    break;
                case "thursday":
                    endWeek = DateTime.Now.Date.AddDays(-4);
                    break;
                case "friday":
                    endWeek = DateTime.Now.Date.AddDays(-5);
                    break;
                case "saturday":
                    endWeek = DateTime.Now.Date.AddDays(-6);
                    break;
                case "sunday":
                    endWeek = DateTime.Now.Date.AddDays(-7);
                    break;
                default:
                    break;
            }
            startWeek = endWeek.Date.AddDays(-7);
            endWeek = Convert.ToDateTime(endWeek.ToShortDateString() + " 23:59:59");

然后LINQ语句分组查询,
       int value = (from a in context.Documents
                         where a.FillTime >= startTime & a.FillTime <= endTime
                         group a by a.UserID into b
                         orderby b.Count() descending
                         select b.Key).FirstOrDefault();

OK,完成了.呵呵

你可能感兴趣的:(LINQ)