Reputation: 163
I am attempting to copy part of a queue to another queue. As a compromise I tried online a way to copy part of a queue to an array.
var values = stocks.Select(s => new DateClose() { Date = s.Date.Date, Close =
s.Close });
var movingAverageQueueAll = new Queue<DateClose>(values);
// take part of the movingAverageQueueAll put in an array
var movingAverageArray = new DateClose[movingAverageQueueAll.Count() -210].Select(h=> new DateClose()).ToArray();
movingAverageQueueAll.CopyTo(movingAverageArray, 210);
The issue is on line "movingAverageQueueAll.CopyTo(movingAverageArray, 210);"
There is not an error but the webpage for the Controller has an internal server error and does not display the data I expect.
Upvotes: 1
Views: 609
Reputation: 1096
Assuming your code comes after the validation movingAverageQueueAll.Count() > 210
as you said the issue is in the last line, you may misunderstand the second parameter of Queue.CopyTo. It is not the starting point in movingAverageQueueAll
but the one in movingAverageArray
where the data are copied to. so, your code always copies original collection of data to a smaller array, and ends up with error.
If your intention is to acquire a subset of data after a certain index, then try this. Skip
and Take
are LINQ methods.
var movingAverageArray = movingAverageQueueAll.Skip(210).Take(movingAverageQueueAll.Count() - 210);
Upvotes: 2