我想对可观察对象应用转换,但我希望某些记录在输出中产生额外的记录。这是我的意思的程序版本:
foreach (var source in sourceList)
{
var destObject = new DestObject(source);
output.Add(destObject);
if (source.Type == 7)
{
var additionalObject = new DestObject {
Field1 = xx,
Field2 = yy,
};
output.Add(additionalObject);
}
}
是否有一个很好的功能性“响应式(Reactive)”方式来做到这一点?我能想到的最好方法是使用 SelectMany,然后基本上使用上面的示例函数来创建一个 1 或 2 个成员列表,然后在 lambda 中返回该列表。这似乎没问题,但我对一般的响应式(Reactive)扩展和函数式编程还很陌生,我只是不知道我是否错过了一种更优雅的方法。
请您参考如下方法:
The best I can come up with is to use SelectMany and then basically use that example function above to create either a 1 or 2 member list and then return that list within the lambda
在我看来是合理的:
sourceList.ToObservable()
.SelectMany(x => Observable.Return(x)
.Concat(x.Type == 7 ? Observable.Return(newThing) : Observable.Empty(x));
