使用 linq 获取分组日期范围
本文关键字:日期 范围 linq 获取 使用 | 更新日期: 2023-09-27 18:35:52
我有这个数据,我必须按价格分组,并检查数据的范围和连续性
date price
2014-01-01 10
2014-01-02 10
2014-01-03 10
2014-01-05 20
2014-01-07 30
2014-01-08 40
2014-01-09 50
2014-01-10 30
输出应如下所示
2014-01-01 2014-01-03 10
2014-01-05 2014-01-05 20
2014-01-07 2014-01-07 30
2014-01-08 2014-01-08 40
2014-01-09 2014-01-09 50
2014-01-10 2014-01-10 30
到目前为止我试过了
var result = list
.OrderBy(a => a.Date)
.GroupBy(a => a.Price)
.Select(x => new
{
DateMax = x.Max(a => a.Date),
DateMin = x.Min(a => a.Date),
Count = x.Count()
})
.ToList()
.Where(a => a.DateMax.Subtract(a.DateMin).Days == a.Count)
.ToList();
我不太确定这是否适合连续日期。所有日期都是独一无二的!
因此,首先我们将使用帮助程序方法对连续的项目进行分组。 它将采用一个函数,该函数将被赋予"上一个"和"当前"项,然后它将确定该项目是否应该在当前组中,或者应该开始一个新组。
public static IEnumerable<IEnumerable<T>> GroupWhile<T>(
this IEnumerable<T> source, Func<T, T, bool> predicate)
{
using (var iterator = source.GetEnumerator())
{
if (!iterator.MoveNext())
yield break;
List<T> list = new List<T>() { iterator.Current };
T previous = iterator.Current;
while (iterator.MoveNext())
{
if (predicate(previous, iterator.Current))
{
list.Add(iterator.Current);
}
else
{
yield return list;
list = new List<T>() { iterator.Current };
}
previous = iterator.Current;
}
yield return list;
}
}
现在,我们能够使用该方法对项目进行分组,然后选择所需的信息:
var query = data.OrderBy(item => item.Date)
.GroupWhile((previous, current) =>
previous.Date.AddDays(1) == current.Date
&& previous.Price == current.Price)
.Select(group => new
{
DateMin = group.First().Date,
DateMax = group.Last().Date,
Count = group.Count(),
Price = group.First().Price,
});
作为Servy的答案的替代方案,我觉得它更优雅,显然更可靠,
您可以在一次扫描中做更多定制的事情(订购后)。
public class ContiguousValuePeriod<TValue>
{
private readonly DateTime start;
private readonly DateTime end;
private readonly TValue value;
public ContiguousValuePeriod(
DateTime start,
DateTime end,
TValue value)
{
this.start = start;
this.end = end;
this.value = value;
}
public DateTime Start { get { return this.start; } }
public DateTime End { get { return this.start; } }
public TValue Value { get { return this.value; } }
}
public IEnumerable<ContiguousValuePeriod<TValue>>
GetContiguousValuePeriods<TValue, TItem>(
this IEnumerable<TItem> source,
Func<TItem, DateTime> dateSelector,
Func<TItem, TValue> valueSelector)
{
using (var iterator = source
.OrderBy(t => valueSelector(t))
.ThenBy(t => dateSelector(t))
.GetEnumerator())
{
if (!iterator.MoveNext())
{
yield break;
}
var periodValue = valueSelector(iterator.Current);
var periodStart = dateSelector(iterator.Current);
var periodLast = periodStart;
var hasTail = false;
while (iterator.MoveNext())
{
var thisValue = valueSelector(iterator.Current);
var thisDate = dateSelector(iterator.Current);
if (!thisValue.Equals(periodValue) ||
thisDate.Subtract(periodLast).TotalDays > 1.0)
{
// Period change
yield return new ContiguousValuePeriod(
periodStart,
periodLast,
periodValue);
periodStart = thisDate;
periodValue = thisValue;
hasTail = false;
}
else
{
hasTail = true;
}
periodLast = thisDate;
}
}
if (hasTail)
{
yield return new ContiguousValuePeriod(
periodStart,
periodLast,
periodValue);
}
}
你像这样使用,
var result = yourList.GetContiguousValuePeriods(
a => a.Date,
a => a.Price);