Linq Groupby 运行时间

Linq Groupby Elapsed Time

我需要按所有者、城镇和 "timeout" 按列表分组。 我会解释 "timeout":

public class Ticket
{
    public int pk { get; set; }
    public string owner { get; set; }
    public string reffering { get; set; }
    public DateTime? created_time { get; set; }
}

我得到了一份清单。 我想创建一个列表>,其中每个子列表都包含一个票证列表,其中 created_time 小于 10 秒。

这是票证示例列表:

pk   owner   reffering   created_time
#1   John    Sam         15/11/2017 11:33:20
#2   John    Sam         15/11/2017 11:33:21
#3   Pat     Jerry       15/11/2017 11:33:27
#4   John    Sam         15/11/2017 11:33:28
#6   Pat     Jerry       15/11/2017 11:33:35
#5   John    Sam         15/11/2017 11:34:00

我需要得到一个列表

   pk   owner   reffering   created_time
---#1 Sub List ---
    #1   John    Sam         15/11/2017 11:33:20
    #2   John    Sam         15/11/2017 11:33:21
    #4   John    Sam         15/11/2017 11:33:28
---#2 Sub List ---
    #5   John    Sam         15/11/2017 11:34:00
---#3 Sub List ---
    #3   Pat     Jerry       15/11/2017 11:33:27
    #6   Pat     Jerry       15/11/2017 11:33:35

这是我的起始代码,但我可以找到如何做到这一点..

List<List<Ticket>> result = tickets.OrderBy(p => p.created_time).GroupBy(p => new { p.owner, p.reffering }).Select(g => g.ToList()).ToList();

希望有人能帮上忙,尤其是关于流逝的时间……这让我抓狂!

以下内容可能有所帮助。 它按 10 秒的固定桶对条目进行分组。

void Main()
{
    var ticketsList = new List<Ticket>();
    ticketsList.Add(new Ticket { pk = 1, owner="John", reffering="Sam"  , created_time = new DateTime(2017, 11, 15, 11, 33, 20) });
    ticketsList.Add(new Ticket { pk = 2, owner="John", reffering="Sam"  , created_time = new DateTime(2017, 11, 15, 11, 33, 21) });
    ticketsList.Add(new Ticket { pk = 3, owner="Pat" , reffering="Jerry", created_time = new DateTime(2017, 11, 15, 11, 33, 27) });
    ticketsList.Add(new Ticket { pk = 4, owner="John", reffering="Sam"  , created_time = new DateTime(2017, 11, 15, 11, 33, 28) });
    ticketsList.Add(new Ticket { pk = 6, owner="Pat" , reffering="Jerry", created_time = new DateTime(2017, 11, 15, 11, 33, 35) });
    ticketsList.Add(new Ticket { pk = 5, owner="John", reffering="Sam"  , created_time = new DateTime(2017, 11, 15, 11, 34, 00) });

    var now = DateTime.Now;


    var orderedList = ticketsList.OrderBy(p => p.created_time).GroupBy(p => new { p.owner, p.reffering }).Select(g => g.ToList());

    // Here is the 10 seconds grouping. I'm basically creating a new date 
    // starting at the beginning of a 10 seconds interval. I will then 
    // use this new date to perform the grouping.
    var normalizedGroupKeysList = ticketsList.Select(t => new { ticket = t, groupKey = t.created_time.HasValue ? t.created_time.Value.AddSeconds(-t.created_time.Value.Second % 10) : now });

    var result = normalizedGroupKeysList.GroupBy(t => t.groupKey, t => t.ticket);
}

public class Ticket
{
    public int pk { get; set; }
    public string owner { get; set; }
    public string reffering { get; set; }

    public DateTime? created_time { get; set; }
}

输出为:

Key=    11/15/2017 11:33:20 AM
pk  owner   reffering   created_time
1   John    Sam 11/15/2017 11:33:20 AM
2   John    Sam 11/15/2017 11:33:21 AM
3   Pat Jerry   11/15/2017 11:33:27 AM
4   John    Sam 11/15/2017 11:33:28 AM      

Key=    11/15/2017 11:33:30 AM
pk  owner   reffering   created_time
6   Pat Jerry   11/15/2017 11:33:35 AM

Key=    11/15/2017 11:34:00 AM
pk  owner   reffering   created_time
5   John    Sam 11/15/2017 11:34:00 AM

如果您想在动态存储桶中按 10 秒为一组对票证进行动态分组,您应该需要一些聚类算法来找到数据的最佳分区。

我不太确定您是否可以使用 LINQ 完成所有这些,这里是我的实现供您参考

var ticketGroups = tickets.OrderBy(x => x.created_time)
                            .GroupBy(x => new { x.owner, x.reffering });
var tensecondTimespam = new TimeSpan(0, 0, 10);
var results = new List<List<Ticket>>();
var ticketTemps = new List<Ticket>();

foreach (var ticketGroup in ticketGroups)
{
    foreach (var ticket in ticketGroup)
    {
        var currentTicket = ticketTemps.LastOrDefault();
        var isWithinTimeRange = currentTicket == null
                                || ticket.created_time.Value - currentTicket.created_time.Value <= tensecondTimespam;

        if (!isWithinTimeRange)
        {
            results.Add(ticketTemps);
            ticketTemps = new List<Ticket>();
        }
        ticketTemps.Add(ticket);
    }
    if (ticketTemps.Any())
    {
        results.Add(ticketTemps);
        ticketTemps = new List<Ticket>();
    }
}

结果变量将保存您的预期输出