简体   繁体   中英

Best way to observe changes to SQL table or query in c#

I have the following repository method...

IEnumerable<Client> repo.GetActiveClients();

What is the best way to periodically call this method and observe changes to the resulting enumerable list?

I am aware that RX provides .ToObservable() , which simplifies responding to changes in collection through subscriptions, but from examples I've seen, I can't work out how best to handle the polling and periodic calling of the method which will change the contents on the collection in the first place?

Unless I'm missing something in your question, I think this is the best option:

IDisposable subscription =
    Observable
        .Interval(TimeSpan.FromSeconds(15.0))
        .Select(x => repo.GetActiveClients())
        .Subscribe(clients =>
        {
            /* do something with the `clients` */
        });

Simple.

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