Reputation: 163
I have two IEnumerable
s, stockDates
and stockClosing
, that I want to place in a queue.
IEnumerable<DateTime> stockDates = stocks.Select(equity => equity.Date);
IEnumerable<decimal> stockClosing = stocks.Select(equity => equity.Close);
// create a queue
Queue<MovingAverage> movingAverageQueue = new Queue<MovingAverage>();
How can I add stockDates
and stockClosing
into the newly created movingAverageQueue
?
Here is the MovingAverage class:
namespace myBackEnd.Models
{
public class MovingAverage
{
public DateTime Date { get; set; }
public decimal Close { get; set; }
}
}
Upvotes: 1
Views: 200
Reputation: 34160
You can use IEnumerable.Zip()
for this, however as the MovingAverage
class is not shown in the post, I just used examplary properties:
movingAverageQueue = stockDates.Zip(stockClosing , (d, c) => new MovingAverage{ Date = d, Average = c});
If they are both in stocks and you want average of closing with a date then you can do this:
var result = stocks.GroupBy(x=> x.Date.Date)
.Select(g => new MovingAverage{ Date = g.Key, Average = g.Average()});
Note that in stocks.GroupBy(x=> x.Date.Date)
as x.Date
is a DateTime, x.Date.Date
will be its date (without time) so that all with the same date would be equal for grouping.
Upvotes: 1
Reputation: 407
You can use one of the Queue constructor Queue(ICollection)
new Queue<MovingAverage>(stocks.Select(stock => new MovingAverage(...))
Upvotes: 0