Linq查询以显示基于月的注册用户数

时间:2019-02-14 04:36:49

标签: c# linq

var list = dbcontext.StudentProfiles.GroupBy(u => u.CreatedDate.Value.Month)
                .Select(u => new UserData
                {
                    // UserCount = u.Count(),
                    Month = u.FirstOrDefault().CreatedDate.Value.Month.ToString()
                    // UserCount = dbcontext.StudentProfiles.Where(u => u.CreatedDate.Value.Month ==0).Count()
                     UserCount = dbcontext.StudentProfiles.Where(u => u.CreatedDate).Count()
                }).ToList();

我需要根据其注册日期显示月度计数,例如:1月:23、2月:86、3月:45等……。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

var list = dbcontext.StudentProfiles.GroupBy(u => u.CreatedDate.Value.Month)
                .Select(group => new UserData
                {
                    Month = group.Key.ToString(),
                    UserCount = group.Count()
                }).ToList();
相关问题