压缩分组的 IObservable


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);

所以groupedtype IObservable<IGroupedObservable<string, IQuote>>

两个问题。

1(我试图

grouped.SortBy(instrument => instrument.Symbol);

SortBy似乎不存在?

2( 假设有两个符号进来,GOOG 和 AAPL 进入grouped。 如何使用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>>

这对你来说是什么样子的?

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)));

相关内容

  • 没有找到相关文章

最新更新