我如何获得一个星期的数据与计数和日期的列表



我试图获得数据的最后一个星期的日期与计数的列表。问题是,如果计数为零的日期不在该列表中,但我想要所有的七天与计数

lastWeekRegisterStatistic1 = dbContext.MtMUsers.Join(dbContext.MtLogRegistrationAudits,
U => U.EmployeeId,
Ru => Ru.UserId,
(U, Ru) => new { U = U, Ru = Ru })
.Where(x => x.Ru.Status != null && 
x.Ru.Status.ToLower() == "success" &&
x.Ru.LoggedDate.Value.Date >= dt.Date &&
x.Ru.LoggedDate.Value.Date <= dt.AddDays(6).Date &&
x.U.IsMobileUser != 0 && 
(request.clientIds == null || 
request.clientIds.Count == 0 ||
request.clientIds.Contains(x.U.ClientId)) &&
(request.assetIds == null || 
request.assetIds.Count == 0 ||
request.assetIds.Contains(x.U.AssetId)) &&
((request.startDate == null && request.endDate == null) ||
(request.startDate <= x.U.CreatedDate && request.endDate >= x.U.CreatedDate)))
.GroupBy(g => new { g.Ru.LoggedDate.Value.Date }).OrderByDescending(o => o.Count())
.Select(s => new lastWeekStatisticsObject
{
date = s.Key.Date,
userCount = s.Count()
}).OrderBy(o => o.date).ToList();

这是我的代码结果是

"lastWeekStatistics": [
{
"date": "2022-11-10T00:00:00",
"userCount": 166
},
{
"date": "2022-11-11T00:00:00",
"userCount": 132
},
{
"date": "2022-11-12T00:00:00",
"userCount": 17
}

我只得到三个日期数据,因为余额日期没有数据

我要所有带有日期和计数的七天

就不能在后面加上不存在的日期吗?查询显示为List?

var emptyDates = Enumerable.Range(0, 6)
.Select(x => dt.Date.AddDays(x))
.Where(x => 
!lastWeekRegisterStatistic1
.Select(y => y.date)
.Contains(x))
.Select(x => new lastWeekStatisticsObject()
{
date = x,
userCount = 0
});

然后您可以使用正常的List操作附加结果:

lastWeekRegisterStatistic1.Add(emptyDates);

不要忘记对List进行排序!

相关内容

  • 没有找到相关文章

最新更新