简体   繁体   中英

How to convert IChangeSet<int> to List<int>?

I need to convert IObservable<int> to IObservable<bool> . To do this I need in last two values of observableSelectedQuantity .

    IObservable<int> observableSelectedQuantity;
    public Constructor()
    {
        observableSelectedQuantity = Item.WhenAnyValue(x => x.SelectedQuantity);
        observableSelectedQuantity.ToObservableChangeSet().Select(ConvertToBool).Subscribe();
    }

    private bool ConvertToBool(IChangeSet<int> arg)
    {
        //Inside logic
    }

I thought that I can get last two elements from some kind of list. So I converted IObservable<int> to IChangeSet<int> and then wrote this code in the ConvertToBool :

var a = arg.ToList();

I expected that each time new element will be added to observableSelectedQuantity , ToList will generate new list with previous elements plus last one. But every time when new element is added, ToList() generate list with only one item which is last element of observableSelectedQuantity . Is it possible to solve my problem with IChangeSet ? Or I need to change my way to get access to list values from observableSelectedQuantity ?

I expected that each time new element will be added to observableSelectedQuantity , ToList will generate new list with previous elements plus last one.

No, it doesn't but you don't need an IChangeSet<int> to keep track of the last two values from an IObservable<int> . You could just store the previous value in a variable, eg:

IObservable<int> observableSelectedQuantity = this.WhenAnyValue(x => x.SelectedQuantity);
IObservable<bool> observableOfBools = observableSelectedQuantity.Select(i => ConvertToBool(i));
...
int? previousValue = null;
private bool ConvertToBool(int currentValue)
{
    bool returnValue = ...; //logic

    previousValue = currentValue;
    return returnValue;
}

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