c# - 压缩分组的 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);
grouped
也是_type
IObservable<IGroupedObservable<string, IQuote>>
两个问题。
1)我试图
grouped.SortBy(instrument => instrument.Symbol);
但SortBy
似乎不存在?
2)假设有两个符号进入,GOOG 和 AAPL 进入grouped
。如何使用Zip
运算符,以便在我Subscribe
是 a时得到什么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)));
推荐阅读
- swift - applicationDidFinishLaunching 没有被触发
- r - gtsummary 中具有计算数据集/加权计数的交叉表
- c++ - 如何读取带有 ESP32 中断和全局标志的旋转编码器
- python-3.x - 音乐机器人的音量只会下降,但不会上升
- javascript - 图像滑块到达尽头,无处可去
- typescript - 在 TypeScript 的子类中自动覆盖多个函数的返回类型
- sql-server - SSIS 调用 Powershell - 无法识别该术语
- c - 将c程序编译为mips二进制文件后出现“无法执行二进制文件:执行格式错误”
- javascript - JustSwap智能合约如何完成swap方法
- flutter - Flutter 在标记指针所在的位置获取 lat 和 long 值