填充缺少日期LINQ的列表
我有以下对象列表
List 包含值的MyList
date high low avg 2014-08-21 16:15:00 20 10 22.5 2014-08-21 16:12:00 21 11 02 2014-08-21 16:09:00 25 12 23 2014-08-21 16:08:00 23 16 22 2014-08-21 16:07:00 19 09 21 2014-08-21 16:04:00 35 20 21.5 2014-08-21 16:03:00 45 25 19.5 2014-08-21 16:00:00 64 20 33.5 2014-08-21 15:56:00 32 25 27.5 public class Percentages { public DateTime Date { get; set; } public decimal High { get; set; } public decimal Low { get; set; } public decimal Average { get; set; } }
可以看出,该列表有一些缺失的分钟。 我的目标是使用上一个日期的值添加缺少的分钟列表。 就像是 :
date high low avg 2014-08-21 16:15:00 20 10 22.5 2014-08-21 16:14:00 21 11 02 2014-08-21 16:13:00 21 11 02 2014-08-21 16:12:00 21 11 02 2014-08-21 16:11:00 25 12 23 2014-08-21 16:10:00 25 12 23 2014-08-21 16:09:00 25 12 23 2014-08-21 16:08:00 23 16 22 2014-08-21 16:07:00 19 09 21 2014-08-21 16:06:00 35 20 21.5 2014-08-21 16:05:00 35 20 21.5 2014-08-21 16:04:00 35 20 21.5 2014-08-21 16:03:00 45 25 19.5 2014-08-21 16:02:00 64 20 33.5 2014-08-21 16:01:00 64 20 33.5 2014-08-21 16:00:00 64 20 33.5 2014-08-21 15:59:00 32 25 27.5 2014-08-21 15:58:00 32 25 27.5 2014-08-21 15:57:00 32 25 27.5 2014-08-21 15:56:00 32 25 27.5
我做了类似的事情(见下文),但似乎有点棘手,可能LINQ会更容易:
Mylist = .... List tempList = new List for (int j = tempList.Count - 1; j> 0; j--) { if ( (tempList[j-1].Date - tempList[j].Date).TotalMinutes >1) { candles.Add(Mylist[j]); } }
这应该工作,我已经使用过LINQ,因为你已经要求了。 一般来说,您的需求在很大程度上取决于连续元素,这通常表明您应该使用普通循环而不是LINQ。
// ensure that it's sorted by date percentages.Sort((p1, p2) => p1.Date.CompareTo(p2.Date)); List newPercentages = new List (); foreach (Percentages percentage in percentages) { Percentages lastPercentage = newPercentages.LastOrDefault(); if (lastPercentage != null) { TimeSpan diff = percentage.Date - lastPercentage.Date; int missingMinutes = (int)diff.TotalMinutes - 1; if(missingMinutes > 0) { var missing = Enumerable.Range(1, missingMinutes) .Select(n => new Percentages { Date = lastPercentage.Date.AddMinutes(n), Average = lastPercentage.Average, High = lastPercentage.High, Low = lastPercentage.Low }); newPercentages.AddRange(missing); } } newPercentages.Add(percentage); }
这是一个作为扩展方法的实现,因此您可以使用其他LINQ方法链接它:
public static class Extensions { public static IEnumerable SubstituteMissingMinutes(this IEnumerable source) { if(source == null) throw new ArgumentNullException("source"); return SubstituteMissingMinutesImpl(source.OrderBy(p => p.Date)).Reverse(); } private static IEnumerable SubstituteMissingMinutesImpl(IEnumerable source) { Percentage previous = null; foreach (var Percentage in source) { if(previous != null) { var counter = previous.Date; while((counter = counter.AddMinutes(1)) < Percentage.Date){ yield return new Percentage{ Date = counter, Low = previous.Low, High = previous.High, Avg = previous.Avg }; } } previous = Percentage; yield return Percentage; } } }
有点像:
var date = DateTime.Parse("21.08.2014"); var nextdate = date.AddDays(1); var list = new List(); for (var i = date; i < nextdate; i = i.AddMinutes(1)) { list.Add(i); } list.RemoveRange(tempList.Select(o=>o.Date).ToList());
现在列表应包含缺失值。
- 使用WS-Security UsernameToken PasswordDigest身份validation方案的WCF客户端使用Axis 2 Web服务时出错
- 以编程方式检查复选框列表中的项目,其中文本等于我想要的
- 为什么Web API不会反序列化,但JSON.Net会?
- MVVMLight UserControl视图模型 – 为每个视图创建新的用户控件实例
- 如何在LINQ GroupBy子句中选择最后一条记录
- 访问IsolatedStorageBackingStore时出现HRESULT 0X80131468的原因
- 如何在Compact Framework中将图标图标添加到标题栏并隐藏底栏。
- 使用linq获取最小值和最大值
- Caliburn.Micro + MEF +现代用户界面:IContent事件