Home > Mobile >  How to get the last not logged-in user's within three weeks backwards?
How to get the last not logged-in user's within three weeks backwards?

Time:10-12

I am trying to get the users who are not logged-in within the last three weeks. I have heard about the Method Min And Max to be used but not sure how. So I would like to get the users who have not logged in last three weeks where LoginDate is the list of user logs:

    var threeWeeksAgo = DateTime.Now.Date.AddDays(-21);
    //userLogins is a table
         var lastLogins = lowUsers.UserLogins
                .Join(lowUsers.Clients, ul => ul.client, c => c.companyName, (ul, c) => new { ClientId = c.clientID, LoginDate = ul.dateTimeCreated, CompanyName = c.companyName })
                .Where(a => a.LoginDate <= threeWeeksAgo);

How can I check LoginDate with list of users who has not logged in the last three weeks.

Data:

Id  UserName LoginDate
1   User1    01/10/2021 13:24
2   User2    02/09/2021 13:24
3   User1    03/10/2021 13:24
4   User1    04/10/2021 13:24
5   User2    03/09/2021 13:24

CodePudding user response:

If LoginDate is of type DateTime you can do the following

 var threeWeeksAgo = DateTime.Now.AddDays(-21);
//userLogins is a table
var notLoggedInUsers = userLogins.Where(a=>a.LoginDate < threeWeeksAgo).ToList();

CodePudding user response:

You need first to group by client, order the dates of that group, and then compare the last login date with three weeks ago.

var threeWeeksAgo = DateTime.Now.Date.AddDays(-21);

//userLogins is a table
var lastLogins = lowUsers.UserLogins
    .Join(lowUsers.Clients, ul => ul.client, c => c.companyName, (ul, c) => new { ClientId = c.clientID, LoginDate = ul.dateTimeCreated, CompanyName = c.companyName })
    .GroupBy(x => x.ClientId)
    .Select(g => new { ClientId = g.Key, LastLoginDate = g.OrderByDescending(x => x.LoginDate).FirstOrDefault()?.LoginDate}) 
    .Where(a => a.LastLoginDate <= threeWeeksAgo);
  • Related