我如何合并连续的时期?

hdo*_*men 2 c# linq algorithm datetime list

是否有一种简单的方法来合并具有相同的连续周期(StartDateto EndDate)Value

输入:

ID  StartDate   EndDate     Value 
1   2014-01-01  2014-01-31  71
2   2014-02-01  2014-02-28  71
3   2014-03-01  2014-03-31  71
4   2014-04-01  2014-04-30  50,12
5   2014-05-01  2014-05-31  50,12
6   2014-06-01  2014-06-30  71
7   2014-08-01  2014-08-31  71     (a month is skipped here)
8   2014-09-01  2014-09-30  71
Run Code Online (Sandbox Code Playgroud)

所以这些行将合并如下:

  • 1,2和3到 01-01-2014 03-31-2014 71
  • 4和5到 2014-04-01 05-31-2014 71
  • 6将保持不变
  • 7和8到 2014-08-01 2014-09-30 71

输出应该是:

StartDate   EndDate     Value 
2014-01-01  2014-03-31  71
2014-04-01  2014-05-31  50,12
2014-06-01  2014-06-30  71
2014-08-01  2014-09-30  71
Run Code Online (Sandbox Code Playgroud)

我试过这个:

public List<PeriodInterval> MergePeriods(List<PeriodInterval> samples)
{
    var merged = samples.OrderBy(s => s.StartDate)
        .ThenBy(s => s.StartDate)
        //select each item with its index
        .Select((s, i) => new
        {
            sample = s, 
            index = i
        })
        // group by date miuns index to group consecutive items
        .GroupBy(si => new
        {
            date = si.StartDate.AddDays(1), 
            content = si.Valeur
        })                    
        .Select(g => new PeriodInterval
        {
            StartDate = g.Min(s => s.StartDate),
            EndDate = g.Max(s => s.EndDate),
            Valeur = g.First().Valeur
        });

    return merged.ToList();
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*kiy 5

创建一个扩展方法,该方法按顺序对某个条件进行批处理,它会检查源序列中的两个连续项:

public static IEnumerable<IEnumerable<T>> SequentialGroup<T>(
    this IEnumerable<T> source, Func<T, T, bool> predicate)
{
    using(var iterator = source.GetEnumerator())
    {
        if (!iterator.MoveNext())
            yield break;

        List<T> batch = new List<T> { iterator.Current };

        while (iterator.MoveNext())
        {
            if (!predicate(batch[batch.Count - 1], iterator.Current))
            {
                yield return batch;
                batch = new List<T>();
            }

            batch.Add(iterator.Current);
        }

        if (batch.Any())
            yield return batch;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此方法,您可以创建具有连续日期和相同值的批量项目:

items.SequentialGroup((a, b) =>
                a.Value == b.Value && (b.StartDate - a.EndDate).Days <= 1)
Run Code Online (Sandbox Code Playgroud)

从这些组创建聚合项很容易.假设您的商品看起来像:

public class Item
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Value { get; set; }
    public string Line { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

查询:

var query = items.SequentialGroup((a, b) =>
    a.Value == b.Value && (b.StartDate - a.EndDate).Days <= 1)
                 .Select((g,i) => new Item {
                     Value = g.First().Value,
                     StartDate = g.Min(f => f.StartDate),
                     EndDate = g.Max(f => f.EndDate),
                     Line = String.Format("mergedLine_{0}", i + 1)
                 });
Run Code Online (Sandbox Code Playgroud)

对于您的示例输入输出将是:

[
  {
    StartDate: "2014-01-01T00:00:00",
    EndDate: "2014-03-31T00:00:00",
    Value: "71",
    Line: "mergedLine_1"
  },
  {
    StartDate: "2014-04-01T00:00:00",
    EndDate: "2014-05-31T00:00:00",
    Value: "50,12",
    Line: "mergedLine_2"
  },
  {
    StartDate: "2014-06-01T00:00:00",
    EndDate: "2014-06-30T00:00:00",
    Value: "71",
    Line: "mergedLine_3"
  },
  {
    StartDate: "2014-08-01T00:00:00",
    EndDate: "2014-09-30T00:00:00",
    Value: "71",
    Line: "mergedLine_4"
  }
]
Run Code Online (Sandbox Code Playgroud)