在循环中组合多个扩展方法

时间:2013-05-22 17:00:51

标签: c#

我有以下课程:

public class Datum
{
    public DateTime DateTime;
    public double Value;
}

使用这些扩展方法:

 public static double Sum(this IEnumerable<Datum> @this)
    {
        return @this.Sum(datum => datum.Value);
    }


 public static IEnumerable<Datum> InTimeRange(this IEnumerable<Datum> @this, DateTime start, DateTime end)
    {
        return from datum in @this
               where (start <= datum.DateTime && datum.DateTime < end)  
               select datum;
    }

 public static IEnumerable<Datum> ForEach(this IEnumerable<Datum> @this, object businesshours)
    {

        var pob = businesshours.ToString();
        switch(pob)
        {
            case "business": 
                return from datum in @this
                       where (datum.DateTime.DayOfWeek != DayOfWeek.Sunday & datum.DateTime.DayOfWeek != DayOfWeek.Saturday & datum.DateTime.Hour > 7 & datum.DateTime.Hour < 20)
                       select datum;

            case "offhours": 
                return from datum in @this
                       where !(datum.DateTime.DayOfWeek != DayOfWeek.Sunday & datum.DateTime.DayOfWeek != DayOfWeek.Saturday & datum.DateTime.Hour > 7 & datum.DateTime.Hour < 20)
                       select datum;
             default:
                return from datum in @this
                       select datum;
        }
    }

public static IEnumerable<Datum> Addition(this IEnumerable<Datum> @this, double val)
    {
        foreach (var datum in @this)
        {
            datum.Value += val;
        }
        return @this;
    }

如果我想总结我可以做的所有工作时间:

somelist.ForEach(busineshourindicator).InTimeRange(startdate, enddate).Sum();

这没有任何问题,如果businessindicator =“business”,它只在给定时间范围内的营业时间内汇总。

但是,如果我想在for循环中组合所有这些并在一小时计划中组合多个数字(如下所示):

        public static double[] hourlyvec(double[] DelStart, double[] DelEnd, double[] position, object[] businessindicator, double PosStart, double PosEnd)
    {
        double nrhrs = (PosEnd - PosStart + 1) * 24;

        List<Datum> result = new List<Datum>();
        double tempdate = PosStart;
        for (int i = 0; i < nrhrs; ++i)
        {
            result.Add(new Datum { DateTime = DateTime.FromOADate(tempdate), Value = 0 });
            tempdate += (1 / 24);
        }


        for (int k = 0; k < DelStart.Length; k++)
        {
            result.ForEach(businessindicator[k]).InTimeRange(DateTime.FromOADate(DelStart[k]), DateTime.FromOADate(DelEnd[k])).Addition(position[k]);
        }

        return result.todoub().ToArray();

    }

这失败了。我期望的快速示例:

第一行: Delstart = 3/1/13,DelEnd = 4/1/13,Businessindicator =“Business”,Position = 100

第二行: Delstart = 3/1/13,DelEnd = 4/1/13,Businessindicator =“Offhours”,位置= 50

第三行: Delstart = 3/1/13,DelEnd = 4/1/13,Businessindicator =“ALL”,Position = 75

Posstart = 3/1/13,PosEnd = 4/1/13

然后该函数应该返回24小时的每小时向量,其中小时1-8 = 125,小时9-20 = 175,剩余小时数再次为125.

所有这些扩展方法似乎都是单独工作的,但是当我将它们组合在一个循环中时,我得不到正确的答案。

对于这个冗长的帖子感到抱歉,我希望我的意思很清楚。

谢谢!

0 个答案:

没有答案