Skip to main content
 首页 » 编程设计

c#之使用 LINQ 压缩事件列表

2024年02月13日11lvdongjie

我在审计类中有一个日志条目列表

public class Audit 
{ 
    public DateTime TimeStamp { get; set; } 
    public string User { get; set; } 
    public string AuditType { get; set; } 
} 

所以列表可能是这样的;

20140206 11:29:20   Owen   Open    
20140206 11:29:21   Owen   Close 
20140206 11:31:20   Owen   Open 
20140206 11:32:20   Owen   Close 
20140206 11:42:20   Owen   Open 
20140206 11:50:00   Owen   Acknowledge 

这给了我们 1 秒、1 分和 40 秒的间隔。所以开的最长时间是中间一对1分钟,然后在11:50确认了。我正在寻找开放时间较长的日期对,在本例中为 1 分钟。

我知道我可以按顺序处理列表并使用 TimeSpan 找到最大的差距,但我认为有一种巧妙的 LINQ 方法可以对组进行处理?

UPDATE 这不是很漂亮,但这是真正扩展步行的逻辑

var audits = notice.AuditEntries.Where(a => a.User == user); 
 
DateTime? currentOpen = null; 
DateTime? bestOpen = null; 
DateTime? bestClose = null; 
foreach (var audit in audits) 
{ 
    if (audit.AuditType == "Open") 
    { 
       if (currentOpen.HasValue) continue; 
       currentOpen = audit.TimeStamp; 
    } 
    if (audit.AuditType == "Close" || audit.AuditType == "Acknowledge") 
    { 
       if (currentOpen.HasValue) 
       { 
          DateTime? currentClose = audit.TimeStamp; 
          if (!bestOpen.HasValue) 
          { 
             bestOpen = currentOpen; 
             bestClose = currentClose; 
          } 
          else 
          { 
             if (bestClose.Value.Subtract(bestOpen.Value) >                                               currentClose.Value.Subtract(currentOpen.Value)) 
             { 
                bestOpen = currentOpen; 
                bestClose = currentClose; 
             } 
           } 
         currentOpen = null; 
         } 
      } 
  } 

请您参考如下方法:

我认为这会成功:

IEnumerable<Audit> audits = ... 
 
var longestAuditsByUser = audits.OrderBy(a => a.Timestamp) 
    // group by user, since presumably we don't want to match an open from one user with a close from another 
    .GroupBy(a => a.User) 
    .Select(userAudits =>  
    { 
        // first, align each audit entry with it's index within the entries for the user 
        var indexedAudits = userAudits.Select((audit, index) => new { audit, index }); 
        // create separate sequences for open and close/ack entries 
        var starts = indexedAudits.Where(t => t.audit.AuditType == "Open"); 
        var ends = indexedAudits.Where(t => t.audit.AuditType == "Close" || t.audit.AuditType == "Acknowledge"); 
 
        // find the "transactions" by joining starts to ends where start.index = end.index - 1 
        var pairings = starts.Join(ends, s => s.index, e => e.index - 1, (start, end) => new { start, end }); 
 
        // find the longest such pairing with Max(). This will throw if no pairings were 
        // found. If that can happen, consider changing this Select() to SelectMany() 
        // and returning pairings.OrderByDescending(time).Take(1) 
        var longestPairingTime = pairings.Max(t => t.end.Timestamp - t.start.Timestamp); 
        return new { user = userAudits.Key, time = longestPairingTime }; 
    }); 
 
// now that we've found the longest time for each user, we can easily find the longest 
// overall time as well 
var longestOverall = longestAuditsByUser.Max(t => t.time);