Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9275523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:36:27+00:00 2026-06-18T16:36:27+00:00

Observable.TakeWhile allows you to run a sequence as long as a condition is true

  • 0

Observable.TakeWhile allows you to run a sequence as long as a condition is true (using a delegate so we can perform computations on the actual sequence objects), but it’s checking this condition BEFORE each element. How can I perform the same check but AFTER each element?

The following code demonstrates the problem

    void RunIt()
    {
        List<SomeCommand> listOfCommands = new List<SomeCommand>();
        listOfCommands.Add(new SomeCommand { CurrentIndex = 1, TotalCount = 3 });
        listOfCommands.Add(new SomeCommand { CurrentIndex = 2, TotalCount = 3 });
        listOfCommands.Add(new SomeCommand { CurrentIndex = 3, TotalCount = 3 });

        var obs = listOfCommands.ToObservable().TakeWhile(c => c.CurrentIndex != c.TotalCount);

        obs.Subscribe(x =>
        {
            Debug.WriteLine("{0} of {1}", x.CurrentIndex, x.TotalCount);
        });
    }

    class SomeCommand
    {
        public int CurrentIndex;
        public int TotalCount;
    }

This outputs

1 of 3
2 of 3

I can’t get the third element

Looking at this example, you may think all I have to do is change my condition like so –

var obs = listOfCommands.ToObservable().TakeWhile(c => c.CurrentIndex <= c.TotalCount);

But then the observable will never complete (because in my real world code, the stream doesn’t end after those three commands)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:36:29+00:00Added an answer on June 18, 2026 at 4:36 pm

    Final edit:

    I based my solution off of Sergey’s TakeWhileInclusive implementation in this thread –
    How to complete a Rx Observable depending on a condition in a event

    public static IObservable<TSource> TakeUntil<TSource>(
            this IObservable<TSource> source, Func<TSource, bool> predicate)
    {
        return Observable
            .Create<TSource>(o => source.Subscribe(x =>
            {
                o.OnNext(x);
                if (predicate(x))
                    o.OnCompleted();
            },
            o.OnError,
            o.OnCompleted
        ));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a observable collection of viewmodel objects. How can I subscribe to the
I'm binding an Observable collection to a combobox using the MVVM pattern.I can successfully
currently im using an observable collection called locationItems to to store my objects from
I have an observable collection of objects that I'd like to display on the
I have a ko.observable property of an object called totalLength. While using application I
I have an observable collection of Song objects. These song objects have a property
Is there a way to creating an observable from a sequence in F#? The
So for example the observable array is set-up using the mapping function (ko.mapping.fromJS). How
I have an observable array containing objects (dynamically created) that have themselves observable attributes.
I have observable objects that update the input values in real time. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.