Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dump latest list in LinqPad?

So the following code will do a dump of the whole list every second.

var list = new List<object>();

for (int i = 0; i < 100; i++)
{
    list.Add(new { A = i.ToString(), B = new Random().Next() });
    list.Dump(); // How to DumpLatest()?
    Thread.Sleep(1000);
}

But how can I make it to just update the dump output without adding a new one?

There is a related Q/A here but it doesn't work for me.

like image 942
orad Avatar asked Feb 09 '23 12:02

orad


2 Answers

The DumpLatest() extension method only applies to IObservable<T>; there's no way to detect that an item is added to a List<T>, so LinqPad can't display the last value added.

Instead you can use a DumpContainer and change its content explicitly:

var list = new List<object>();

var container = new DumpContainer();
container.Dump();

for (int i = 0; i < 100; i++)
{
    var item = new { A = i.ToString(), B = new Random().Next() };
    list.Add(item);
    container.Content = item;
    Thread.Sleep(1000);
}

You could also achieve the same result with a Subject<T> (arguably more elegant):

var subject = new Subject<object>();
subject.DumpLatest();

for (int i = 0; i < 100; i++)
{
    var item = new { A = i.ToString(), B = new Random().Next() };
    subject.OnNext(item);
    Thread.Sleep(1000);
}

EDIT: OK, I thought you wanted to see only the last item. To print the whole list, just use subject.Dump(), as mentioned by Joe in the comments. If you use the first approach, put the list itself in the DumpContainer, and call Refresh() on it in the loop.

like image 88
Thomas Levesque Avatar answered Feb 12 '23 10:02

Thomas Levesque


Basically same with Thomas Levesque's answer, a little shorter.

Observable.Interval(TimeSpan.FromSeconds(1))
.Select(t=> new { A = t.ToString(), B = new Random().Next() })
.Take(100)
.Dump(); // all 100 
//.DumpLatest(); //only latest one
like image 31
Rm558 Avatar answered Feb 12 '23 11:02

Rm558