压缩分组的IObservable

时间:2019-06-02 02:01:16

标签: c# system.reactive

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)    假设grouped中有两个符号,GOOG和AAPL。如何使用Zip运算符,以便在SubscribeTuple<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>>

1 个答案:

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