缺少日期LINQ的填充列表 [英] Populate list with missing dates LINQ

查看:79
本文介绍了缺少日期LINQ的填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象列表

列表<百分比>包含值的MyList

List < Percentages > MyList which contains values

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

可以看出,该列表缺少一些分钟.我的目标是添加缺少的分钟以列出上一日期的值.像这样的东西:

As can be seen the list has some missing minutes . My goal is to add missing minutes to list with the values of previous date. Something like :

            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似乎有点棘手:

I did something like this (see below) but it seems a bit tricky probably with LINQ would be easier :

Mylist < Percentages > = ....
List< Percentages > tempList = new List <Percentages > 
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.

This should work, i've used LINQ since you've asked for. In general your requirement depends heavily on consecutive elements which is often an indication that you should use a plain loop instead of LINQ.

// ensure that it's sorted by date
percentages.Sort((p1, p2) => p1.Date.CompareTo(p2.Date));
List<Percentages> newPercentages = new List<Percentages>();
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的填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆