.NET. Select Users Users Registered Yesterday with Entity Framework


Here is how to select all users registered yesterday using the Entity Framework:

Language: Csharp
var yesterday = DateTime.Now.AddDays(-1);

var yesterdayStart = yesterday.Date.ToString("yyyy-MM-dd 00:00:00"); // SQL formatted
var yesterdayEnd = yesterday.Date.ToString("yyyy-MM-dd 23:59:59"); // SQL formatted

var users = db.AccountUsers
    .Where(u => u.Created >= DateTime.Parse(yesterdayStart) && u.Created <= DateTime.Parse(yesterdayEnd))
    .ToList();
Debug.WriteLine(users.Count);