Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
private static void GetValueIObservable2()
var repo = new AccountRepository();
var accounts = repo.GetAccountWithSubs("123123123");
//create a cold observable from IEnumerable...
var accountObservable = accounts.ToObservable();
//you can filter an IObservable just like an IEnumerable...
//.Where(a => a.AccountNumber.ToString().StartsWith("2"));
//create an observer that has the appropriate delegates... In this case we are only using the OnNext
var observer = Observer.Create<Account>(acc =>
//subscribe to the observable... Since this is a cold observable that wraps an IEnumerable, the act of calling subscribe will iterate through the IEnumerable and call OnNext delegate for each.
var subscription = accountObservable.Subscribe(observer);
// instead of explicitly creating an observer, you can use this extension method when subscribing to just pass in the delegates.
//var subscription = accountObservable.Subscribe(
// acc => Console.WriteLine(acc.AccountNumber),
// ex => { },
// () => { });
public class AccountRepository
public IEnumerable<Account> GetAccountWithSubs(string accountNumber)
yield return GetAccountAsync(accountNumber).Result;
for (int i = 1000; i < 1200; i++)
yield return GetAccountAsync(i.ToString()).Result;
public async Task<Account> GetAccountAsync(string accountNumber)
await Task.Delay(30);
return new Account() {AccountNumber = accountNumber};
public class Account
public string AccountNumber { get; set; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.