EF lambda 分组查询中DateTime转Date

现有的需求是把数据按照店铺编号和申请时间进行分组查询,分组的时候需要把申请时间去掉时间部分,仅保留有日期部分,试了很多都无果,期间还遇到了“无效的匿名类型成员声明符。匿名类型成员必须使用赋值、简单名称或成员访问来声明。”这样的提示错误,还是基础没打好,简单的弄个变量,赋值就可以了,主要用到了方法:DbFunctions.CreateDateTime()。下面把分组查询的代码贴出来

public List<BubbleGoodsByGroup> GetByGroup()
        {
            var query = _db.BubbleGoods
                .Where(a => a.AuditState == 0)
                .Where(u => ShopId == null || u.Uid == ShopId)
                .Where(u => GoodsId == null || GoodsId == 0 || u.GoodsSpecPrice.GoodsId == GoodsId)
                .Where(u => CreateBeginDate == null || u.CreateDate >= CreateBeginDate)
                .Where(u => CreateEndDate == null || u.CreateDate <= CreateEndDate).AsQueryable();
            var model = query.GroupBy(o => new { o.Uid, tempNewDate = DbFunctions.CreateDateTime(o.CreateDate.Year, o.CreateDate.Month, o.CreateDate.Day, 0, 0, 0) })
                .Select(o => new BubbleGoodsByGroup
                {
                    Uid = o.Key.Uid,
                    TotalApplyCount = o.Sum(a => a.ApplyCount),
                    TotalCount = o.Count(a => a.Id > 0),
                    ApplyDate = (DateTime)o.Key.tempNewDate
                })
                .OrderBy(u => u.Uid).ThenBy(u => u.ApplyDate)
                .Skip(PageSize * (PageIndex - 1))
                .Take(PageSize)
                .ToList();
            return model;
        }

参考链接:https://www.cnblogs.com/farb/p/EFSkillsCollection.html

posted @ 2018-02-08 15:18  wangbg  阅读(2418)  评论(0编辑  收藏  举报