简体   繁体   中英

Iterating through an observable list?

I've inherited an api that accesses remote (real time) json via observable subscriptions.

In large part a lot of the members are just strings and doubles but a few are lists and I have zero idea how to iterate through them via linq/takewhile and subscribe methods.

mcSub.TakeWhile(p => p.Status == Market.OPEN).Subscribe(x => Console.WriteLine(DateTime.Now.Second + " - " + mc.Event.Name + " - " + x.Items[0].Prices));

Where x.Items is a list, how would I iterate through that to display prices for all Items in a single statement?

Thanks in advance.

mcSub.TakeWhile(p => p.Status == Market.OPEN).Subscribe(x => x.Items.ForEach(y => Console.WriteLine(DateTime.Now.Second + " - " + mc.Event.Name + " - " + y.Prices)));

*upvote on ThreeFx

An alternate answer would be to flatmap/selectmany the inner list to be pushed as individual items (if this meets your need)

mcSub.TakeWhile(p => p.Status == Market.OPEN)
     .SelectMany(x=>x.Items)
     .Subscribe(item => Console.WriteLine(item.Prices));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM