public delegate void QuoteChangeEvent(IQuote q);
var priceChangedObservable = Observable.FromEvent<QuoteChangeEvent, IQuote>
(handler =>
{
QuoteChangeEvent qHandler = (e) =>
{
handler(e);
};
return qHandler;
},
qHandler => api.MAPI.OnQuoteChange += qHandler,
qHandler => api.MAPI.OnQuoteChange -= qHandler);
var grouped = priceChangedObservable
.GroupBy(instrument => instrument.Symbol);
所以grouped
是type
IObservable<IGroupedObservable<string, IQuote>>
两个问题。
1)我试图
grouped.SortBy(instrument => instrument.Symbol);
但是SortBy
似乎不存在吗?
2)
假设grouped
中有两个符号,GOOG和AAPL。如何使用Zip
运算符,以便在Subscribe
是Tuple<IQuote, IQuote>
时得到什么?
我不太了解正确的语法。像这样:
Observable.Zip(?, ?, (a, b) => Tuple.Create(a, b))
.Subscribe(tup => Console.WriteLine("Quotes: {0} {1}", tup.item1, tup.item2));
编辑1
我几乎明白了:
var first = grouped.Where(group => group.Key == "GOOG").FirstAsync();
var second = grouped.Where(group => group.Key == "AAPL").FirstAsync();
Observable.Zip(first, second, (a, b) => Tuple.Create(a, b))
.Subscribe(tup => Console.WriteLine("Quotes: {0} {1}", tup.Item1, tup.Item2));
问题是tup
的类型不是<IQuote, IQuote>
,而是类型:
Tuple<IGroupedObservable<string, IQuote>, IGroupedObservable<string, IQuote>>
答案 0 :(得分:0)
您觉得这样吗?
IObservable<(IQuote, IQuote)> results =
grouped
.Publish(gs =>
gs
.Where(g => g.Key == "GOOG")
.SelectMany(g => g)
.Zip(
gs
.Where(g => g.Key == "AAPL")
.SelectMany(g => g),
(x, y) => (x, y)));