简体   繁体   中英

How to extract fields of IObservable in Current in C#

Consider me new to the world of C# and the lingo associated with IObservable lingo. I have a question on how to extract the info from Current. Here is my c# code:

    private static IObservable<T> ToObservableInterval<T>(IEnumerable<T> source, 
        TimeSpan period, System.Reactive.Concurrency.IScheduler scheduler)

        {
            return Observable.Using(
                () => source.GetEnumerator(),
                it => Observable.Generate(
                    default(object),
                    _ => it.MoveNext(),
                    _ => _,
                    _ =>
                  {
                        Console.WriteLine("Input {0}", it.Current);
                        return it.Current;
                  },
                    _ => period, scheduler));
    }

If I pass the values of this class in the source:

class LogEvent
    {
        public DateTime X { get; set; }
        public int Id { get; set; }
        public string ServerName { get; set; }
        public string Level { get; set; }

       public override string ToString()
       {
           return new { x, Id, ServerName, Level}.ToString();
       }
}

Is there a easy way to extract values of either x, Id, Level out of the Current representation?

Thanks

Don't you just want to do something like this?

var query =
    from le in ToObservableInterval(items, timeSpan, scheduler)
    select le.ServerName;

Enigmativity has answered the question however, I think it should be pointed out that you probably dont have to put so much effort in, when you could just use Observable.Interval and the Zip operator to this work.

Here is a LinqPad script that you can use to see that it will only pull values from the IEnumerable<T> as each period of time passes.

void Main()
{
    //GetNumbers().Dump();
    var period = TimeSpan.FromSeconds(1);
    var scheduler = Scheduler.Default;
    var source = GetNumbers();
    Observable.Interval(period, scheduler)
            .Zip(source, (a,b)=>b)
            .Dump();

}

// Define other methods and classes here

public IEnumerable<int> GetNumbers()
{
    var i = 0;
    while (i<10)
    {
        DateTimeOffset.Now.ToString("o").Dump();
        yield return i++;
    }
}

So you can reduce your ToObservableInterval method to just:

private static IObservable<T> ToObservableInterval<T>(
            IEnumerable<T> source, 
            TimeSpan period, 
            System.Reactive.Concurrency.IScheduler scheduler)
    {
        return Observable.Interval(period, scheduler)
                         .Zip(source, (a,b)=>b);
    }

and then consume as per the answer above.

As the method is so small, you may even question if you need it at all?

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