I have this piece of code:
if (DateTime.Now.Day == 1)
{
var customers = db.Customers.ToList();
foreach (var customer in customers)
{
var logs = db.Logs.Where(x => x.CustomerId == customer.Id)
.Where(x => x.LogType == LogTypes.RecordLog)
.Where(x => DateTime.Compare(x.CreationDate, DateTime.Today.AddMonths(-1)) >= 0)
.Where(x => DateTime.Compare(x.CreationDate, DateTime.Today) < 0).ToList();
var newLog = new Log()
{
CustomerId = customer.Id,
LogType = LogTypes.MonthlyLog,
LogText = $"{customer.Title} har indsat {logs.Count} elementer imellem d.{DateTime.Now.AddMonths(-1).Date} og d.{DateTime.Now.AddDays(-1).Date}"
};
db.Logs.Add(newLog);
}
await db.SaveChangesAsync();
}
return true;
}
Basically it's running inside of a cron job that runs every day at midnight. It's supposed to work in the following way:
- Check if it's the first day of the month
- Get a list of all record logs dating one month back
- Make a monthly report for each customer and save it to the database
But for some reason no logs are being created? I've been looking at this piece of code endlessly and can't find my mistake, I need fresh eyes on it.
Thank in advance