我如何合并连续的时期?

时间:2014-05-24 12:58:04

标签: c# algorithm linq list datetime

是否有一种简单的方法可以合并具有相同StartDate的连续句点(EndDateValue)?

输入:

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

所以这些行将合并如下:

  • 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

我试过这个:

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();
}

1 个答案:

答案 0 :(得分: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;
    }
}

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

items.SequentialGroup((a, b) =>
                a.Value == b.Value && (b.StartDate - a.EndDate).Days <= 1)

从这些组中创建聚合项很容易。假设您的商品如下:

public class Item
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Value { get; set; }
    public string Line { get; set; }
}

查询:

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)
                 });

对于您的样本输入输出将是:

[
  {
    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"
  }
]