0

我正在传入一个我想要 UNTOUCHED 的对象数组,但由于某种原因,无论我尝试什么,传入的原始数组都被更改了。

private DSPPositionPeriodDO[] CompactShortTermData(DSPPositionPeriodDO[] data)
{
    DSPPositionPeriodDO[] resultArray = (DSPPositionPeriodDO[])data.Clone();
    int? hoursToAggBy = null;
    // it is short term, the data gets too much so decide how many
    // hours to aggregate by.
    var days = (_endDate - _startDate).TotalDays;
    if (days <= 3)
        hoursToAggBy = null;
    else if (days > 3 && days <= 7)
        hoursToAggBy = 1;
    else if (days > 7 && days <= 14)
        hoursToAggBy = 2;
    else if (days > 14 && days <= 21)
        hoursToAggBy = 3;
    else if (days > 21)
        hoursToAggBy = 4;

    if (hoursToAggBy != null)
    {
        // round down hours
        foreach (var posPeriod in resultArray)
        {
            var sl = posPeriod.StartLocal;
            sl = sl.AddHours(-(sl.Hour % hoursToAggBy.Value));
            sl = sl.AddMinutes(-sl.Minute);
            posPeriod.StartLocal = sl;
        }

        // group by entity & time -- get average
        var groupedData = resultArray.GroupBy(x => new
        {
            x.EntityId,
            x.EntityCode,
            x.StartLocal
        })
        .Select(g => new DSPPositionPeriodDO
        {
            EntityId = g.Key.EntityId,
            EntityCode = g.Key.EntityCode,
            StartLocal = g.Key.StartLocal,
            Volume = g.Average(v => v.Volume),
            AverageVolume = null
        });

        resultArray = groupedData.ToArray();
    }
}

我试图创建一个单独的副本,例如:

var x = original.ToArray()

toList()

Clone()

等等

original我的意思是传入的对象。data我希望它根本不被改变。

4

1 回答 1

1

来自Array.Clone

新 Array 中的引用指向的对象与原始 Array 中的引用指向的对象相同。

您需要将每个对象克隆到新数组中。

于 2013-06-21T11:42:44.093 回答