C# 如何合并连续的句点?

C# 如何合并连续的句点?,c#,algorithm,linq,list,datetime,C#,Algorithm,Linq,List,Datetime,有没有一种简单的方法可以合并具有相同值的连续时段(StartDate)到EndDate 输入: ID开始日期结束日期值 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 7

有没有一种简单的方法可以合并具有相同值的连续时段(
StartDate
)到
EndDate

输入:

ID开始日期结束日期值
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(此处跳过一个月)
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值
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();
}
公共列表合并周期(列表示例)
{
var merged=samples.OrderBy(s=>s.StartDate)
.ThenBy(s=>s.StartDate)
//选择每个项目及其索引
.选择((s,i)=>新建
{
样本=s,
指数=i
})
//按日期分组miuns索引以对连续项目进行分组
.GroupBy(si=>新建)
{
日期=si.StartDate.AddDays(1),
content=si.Valeur
})                    
.选择(g=>new PeriodInterval
{
起始日期=g.Min(s=>s.StartDate),
EndDate=g.Max(s=>s.EndDate),
Valeur=g.First().Valeur
});
返回merged.ToList();
}

创建扩展方法,该方法根据某些条件对序列项进行批处理,检查源序列中的两个序列项:

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;
    }
}
查询:

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

你试过什么吗?显示您的尝试。我已尝试将具有相同索引的时段分组
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"
  }
]