简体   繁体   English

如何倒退三周内最后一个未登录的用户?

[英]How to get the last not logged-in user's within three weeks backwards?

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.我听说过要使用的方法 Min 和 Max,但不确定如何使用。 So I would like to get the users who have not logged in last three weeks where LoginDate is the list of user logs:所以我想获取过去三周未登录的用户,其中LoginDate是用户日志列表:

    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.如何使用过去三周未登录的用户列表检查 LoginDate。

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

If LoginDate is of type DateTime you can do the following如果LoginDateDateTime类型,您可以执行以下操作

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM