Search code examples
c#frameworksentity

Entity Framework check if last login time is greater than two days


I am trying to write a query, where I select user and I also want to check if his last login time is greater than two days. If yes, then I want to block this user, if no, the user remains not blocked.

Code snippet :

var query = context.Users.Where(x => !x.NotBlocked && x.LastLoginTime.Value.Days >= 2); 

But unfortunately I know that code is wrong and I am currently looking to write it in a correct way using Entity Framework.


Solution

  • You can use DateTime.Now.AddDays(numberOfDays) to get the exact last values in the last 24 hour * numberOfDays

    var query = context.Users.Where(x => !x.NotBlocked && x.LastLoginTime >= DateTime.Now.AddDays(-2));