Optimal LINQ query to get a random sub collection – Shuffle

Further to mquander’s answer and Dan Blanchard’s comment, here’s a LINQ-friendly extension method that performs a Fisher-Yates-Durstenfeld shuffle: // take n random items from yourCollection var randomItems = yourCollection.Shuffle().Take(n); // … public static class EnumerableExtensions { public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) { return source.Shuffle(new Random()); } public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng) … Read more

Difference between ObservableCollection and BindingList

An ObservableCollection can be updated from UI exactly like any collection. The true difference is rather straightforward: ObservableCollection<T> implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated. However, BindingList<T> implements IBindingList. IBindingList provides notification on collection changes, … Read more

ObservableCollection Doesn’t support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

Please refer to the updated and optimized C# 7 version. I didn’t want to remove the VB.NET version so I just posted it in a separate answer. Go to updated version Seems it’s not supported, I implemented by myself, FYI, hope it to be helpful: I updated the VB version and from now on it … Read more

How do I update an ObservableCollection via a worker thread?

New option for .NET 4.5 Starting from .NET 4.5 there is a built-in mechanism to automatically synchronize access to the collection and dispatch CollectionChanged events to the UI thread. To enable this feature you need to call BindingOperations.EnableCollectionSynchronization from within your UI thread. EnableCollectionSynchronization does two things: Remembers the thread from which it is called … Read more

ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

I’ve put together what I hope is a pretty robust solution, including some of the techniques in other answers. It is a new class derived from ObservableCollection<>, which I’m calling FullyObservableCollection<> It has the following features: It adds a new event, ItemPropertyChanged. I’ve deliberately kept this separate from the existing CollectionChanged: To aid backward compatibility. … Read more

tech