比较连续的行

本文关键字:连续 比较 | 更新日期: 2023-09-27 17:50:45

我有一个数据集,我从DB提取,现在我想验证每个事件的每个连续的subbevent是否发生在24小时(1天)

表:

EventId     Event    SubEvent       EventDate
 1          A      SubEvent_A1   01-10-2013 11:00 hrs  
 2          A      SubEvent_A2   02-10-2013 10:00 hrs  
 3          B      SubEvent_B1   02-11-2013 13:00 hrs  
 4          B      SubEvent_B2   03-11-2013 05:00 hrs  
 5          B      SubEvent_B3   04-11-2013 07:00 hrs   
 6          C      SubEvent_C1   05-11-2013 21:00 hrs  
 7          C      SubEvent_C2   06-11-2013 23:00 hrs
 8          A      SubEvent_A3   04-10-2013 23:00 hrs

执行此检查后,我的结果摘要应显示为:

事件A的subbevent_a3在subbevent_a2发生后1天内未发生
事件B的subbevent_b3未在subbevent_b2的1天内发生
事件C的subbevent_c2未在subbevent_c1的1天内发生

有人能帮忙吗?

比较连续的行

正如您所说的DataSet,此解决方案基于以下假设:您有一个DataSet对象,并且表存储在变量dt中。这个解决方案利用了。net 4函数zip

var q = (
            from r in dt.Rows.Cast<DataRow>()
            orderby r["DateTime"]
            group r by r["Event"] into g
            select g.Zip(g.Skip(1), (DataRow x,DataRow y) => new {                              Event = y["Event"],
                SubEvent1=x["SubEvent"],
                SubEvent2=y["SubEvent"],
                Diff = (DateTime)y["DateTime"] - (DateTime)x["DateTime"]
        })
         ).SelectMany (x => x)
          .Where(x => x.Diff > TimeSpan.FromDays(1))
          .Select(x => string.Format("{0} of Event {1} does not occur within 1 day of {2}",
                                     x.SubEvent2,
                                     x.Event,
                                     x.SubEvent1));

q现在将包含错误消息列表

.Net 3.5的Zip实现

public static class ZipLinqExtension {
  public static IEnumerable<TResult> Zip<T1,T2,TResult>(this IEnumerable<T1> source1, 
                                                             IEnumerable<T2> source2,
                                                             Func<T1,T2,TResult> function) {
     using (var e1 = source1.GetEnumerator()) {
        using (var e2 = source2.GetEnumerator()) {
           while (e1.MoveNext() && e2.MoveNext()) {
              yield return function(e1.Current, e2.Current);
           }
        }
     }
  }

}