0

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

heavy rocker dude
  • 2,271
  • 8
  • 33
  • 47
  • Can you explain what problem you are trying to solve? It seems like you just want to observe the values in the enumerable stream with a small delay between each value, which you can do with: `enumerableSequence.ToObservable().Delay(period, scheduler)` – Brandon Oct 01 '13 at 20:04

2 Answers2

0

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

var query =
    from le in ToObservableInterval(items, timeSpan, scheduler)
    select le.ServerName;
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

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?

Lee Campbell
  • 10,631
  • 1
  • 34
  • 29