If you want the last element to be included you can merge a stream with only the last element together with the regular stream combined with TakeWhile
.
Here is a simple console app to prove it:
var subject = new List<string>
{
"test",
"last"
}.ToObservable();
var my = subject
.Where(x => x == "last").Take(1)
.Merge(subject.TakeWhile(x => x != "last"));
my.Subscribe(
o => Console.WriteLine("On Next: " + o),
() => Console.WriteLine("Completed"));
Console.ReadLine();
This prints:
On Next: test
On Next: last
Completed
UPDATE
There was a bug that supressed the OnCompleted message if the underlying Observable didn't actually complete. I corrected the code to ensure OnCompleted
gets called
And if you want to avoid subscribing to the underlying sequence multiple times for cold observables you can refactor the code like this:
var my = subject.Publish(p => p
.Where(x => x == "last").Take(1)
.Merge(p.TakeWhile(x => x != "last")));