Javascript 按属性和时间间隔对数据进行分组

Javascript 按属性和时间间隔对数据进行分组,javascript,c#,typescript,Javascript,C#,Typescript,我有一门课看起来像这样: public class UserAction { public int LogId { get; set; } public string UserName { get; set; } public CrudAction Action { get; set; } public DateTime Date{ get; set; } } User User action Date and t

我有一门课看起来像这样:

public class UserAction
{
        public int LogId { get; set; }
        public string UserName { get; set; }
        public CrudAction Action { get; set; }
        public DateTime Date{ get; set; }
}
User        User action  Date and time of action
Lisa        Edited       2018-05-20 09:46 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:16 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 10:12 AM GMT+2 
Lisa        Edited       2018-05-16 13.28 PM GMT+2
Lisa        Posted       2018-05-16 13.25 PM GMT+2
User        User action  Date and time of action
Lisa        Edited       2018-05-20 09:46 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 10:12 AM GMT+2 
Lisa        Edited       2018-05-16 13.28 PM GMT+2
Lisa        Posted       2018-05-16 13.25 PM GMT+2
此类的目的是在我的web应用程序中存储有关谁对表单执行了更改的信息。在表单上,您可以打开一个模式,其中显示这些更改以及更改发生的时间

现在,问题是表单具有自动保存功能,它在每次更改后保存表单,这可以在短时间间隔内生成大量日志,因此看起来有点混乱。当我打开modal查看日志时,它通常看起来像这样:

public class UserAction
{
        public int LogId { get; set; }
        public string UserName { get; set; }
        public CrudAction Action { get; set; }
        public DateTime Date{ get; set; }
}
User        User action  Date and time of action
Lisa        Edited       2018-05-20 09:46 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:16 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 10:12 AM GMT+2 
Lisa        Edited       2018-05-16 13.28 PM GMT+2
Lisa        Posted       2018-05-16 13.25 PM GMT+2
User        User action  Date and time of action
Lisa        Edited       2018-05-20 09:46 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 10:12 AM GMT+2 
Lisa        Edited       2018-05-16 13.28 PM GMT+2
Lisa        Posted       2018-05-16 13.25 PM GMT+2
我正在寻找一种聪明的方法,以某种方式将同一个人在同一小时内以相同的动作生成的所有帖子分组,只显示最新的帖子。大概是这样的:

public class UserAction
{
        public int LogId { get; set; }
        public string UserName { get; set; }
        public CrudAction Action { get; set; }
        public DateTime Date{ get; set; }
}
User        User action  Date and time of action
Lisa        Edited       2018-05-20 09:46 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 11:16 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 11:15 AM GMT+2
Tom         Edited       2018-05-18 10:12 AM GMT+2 
Lisa        Edited       2018-05-16 13.28 PM GMT+2
Lisa        Posted       2018-05-16 13.25 PM GMT+2
User        User action  Date and time of action
Lisa        Edited       2018-05-20 09:46 AM GMT+2
Tom         Edited       2018-05-18 11:18 AM GMT+2
Tom         Edited       2018-05-18 10:12 AM GMT+2 
Lisa        Edited       2018-05-16 13.28 PM GMT+2
Lisa        Posted       2018-05-16 13.25 PM GMT+2
我的应用程序是angular 5应用程序,我使用.Net核心Web API从SQL数据库获取数据。我想这种逻辑可以在angular应用程序或web api中实现,但在客户端实现这一点可能更可取


我已经尝试在javascript和LINQ中实现这一点,但我还没走多远(有些东西没有在我的大脑中点击),因此非常感谢您的帮助。

如果您永远不想显示通过分组过滤掉的数据,最好先过滤服务器端的数据。原因是,否则您将向客户端传输不必要的数据,但无论如何都要删除它们。这会减慢你的应用程序的速度。只传输您需要的数据

在WebAPI控制器中,您可以使用LINQ的强大功能。诀窍是引入一个定制的
IEqualityComparer
,它实现您的需求,然后将其提供给
Distinct
方法

UserAction[] userActions = dbContext.UserActions.ToArray();
var distinctActions = userActions
    .OrderByDescending(ua => ua.Date)             // sort as you like here
    .Distinct(new UserNameActionAndHourComparer());   // apply filter

public class UserNameActionAndHourComparer : IEqualityComparer<UserAction> {

    public bool Equals(UserAction left, UserAction right){
        if (left == null && right == null) {
             return true;
        }
        if (left == null | right == null) {
            return false;
        }

        // your equality criteria here ...
        if (left.UserName == right.UserName && left.Action == right.Action) {
            // implement requirement that same hour is considered equal
            return left.Date.Date == right.Date.Date && left.Date.Hour == right.Date.Hour;
        }
        return false;
   }

    // if two UserActions are considered equal, they should yield the same hashcode
    public int GetHashCode(UserAction ua) {
        return ua.UserName.GetHashCode() 
            ^ ua.Action.GetHashCode() 
            ^ new DateTime(ua.Date.Year, ua.Date.Month, ua.Date.Day, ua.Date.Hour, 0, 0).GetHashCode();
    }
}
UserAction[]userActions=dbContext.userActions.ToArray();
var distinctActions=userActions
.OrderByDescending(ua=>ua.Date)//在这里按需要排序
.Distinct(新用户名操作和小时比较());//应用过滤器
公共类用户名ActionAndHourComparer:IEqualityComparer{
public bool Equals(UserAction左,UserAction右){
if(left==null&&right==null){
返回true;
}
如果(左==null |右==null){
返回false;
}
//你的平等标准在这里。。。
if(left.UserName==right.UserName&&left.Action==right.Action){
//实施同一小时被视为相等的要求
返回left.Date.Date==right.Date.Date&&left.Date.Hour==right.Date.Hour;
}
返回false;
}
//如果两个用户操作被认为是相等的,那么它们应该产生相同的哈希代码
公共int GetHashCode(用户操作ua){
返回ua.UserName.GetHashCode()
^ua.Action.GetHashCode()
^新的日期时间(ua.Date.Year,ua.Date.Month,ua.Date.Day,ua.Date.Hour,0,0).GetHashCode();
}
}