我需要按照所有者,城镇和“超时”按列表分组。 我会解释“超时”:
public class Ticket
{
public int pk { get; set; }
public string owner { get; set; }
public string reffering { get; set; }
public DateTime? created_time { get; set; }
}
我得到了一份清单。 我想创建一个List>其中每个子列表包含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();
希望有些人可以帮助特别是经过的时间..它让我疯了!
答案 0 :(得分:1)
这可能有所帮助。 它通过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秒组动态动态分组故障单,则应该需要一些群集算法来查找数据的最佳分区。
答案 1 :(得分:0)
我不太确定你能用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>();
}
}
结果变量将保留您的预期输出