阵列克隆&;复制静态效果原件
本文关键字:静态 复制 amp 阵列 | 更新日期: 2023-09-27 18:19:26
我正在传入一个对象数组,我希望该数组被取消,但由于某种原因,无论我尝试什么,传入的原始数组都会被更改。
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
对象。我希望它根本不要更改。
来自Array.Clone
:
新数组中的引用指向与原始数组中引用指向的对象相同的对象。
您需要将每个对象克隆到新阵列中。