是否有一种简单的方法来合并具有相同StartDate的连续句点(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因此,这些行将按以下方式合并:
01-01-2014 03-31-2014 712014-04-01 05-31-2014 712014-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();
}发布于 2014-05-24 13:42:44
创建扩展方法,该方法根据某些条件对顺序项进行批处理,该方法检查源序列中的两个顺序项:
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"
}
]https://stackoverflow.com/questions/23845306
复制相似问题