Table of Contents

Class ObservableCollectionExtensions

Namespace
DotNetQuery.Mvvm
Assembly
DotNetQuery.Mvvm.dll

Reconciles an ObservableCollection<T> in place against a new source sequence — computing the minimal Move, Insert, Remove, and (same-type overload only) Replace operations needed to match it, rather than clearing and re-adding. Clearing and re-adding drops whatever a bound control tracks by collection position — the selected row in a WPF/Avalonia list, for instance.

public static class ObservableCollectionExtensions
Inheritance
ObservableCollectionExtensions
Inherited Members

Methods

SyncFrom<T, TKey>(ObservableCollection<T>, IEnumerable<T>, Func<T, TKey>, IEqualityComparer<T>?)

Reconciles target to match source in place, keyed by keySelector. An item whose key is no longer present is removed; a new key is inserted; an existing key is moved to its new position and, when itemComparer reports it changed, replaced with the new value from source — there is no way to update T in place here, unlike the projection overload's update. When source already matches target exactly, nothing is raised.

Worst case is O(n²) moves — acceptable for UI-sized lists, not for large ones.

public static void SyncFrom<T, TKey>(this ObservableCollection<T> target, IEnumerable<T> source, Func<T, TKey> keySelector, IEqualityComparer<T>? itemComparer = null) where TKey : notnull

Parameters

target ObservableCollection<T>

The collection to reconcile in place.

source IEnumerable<T>

The desired contents, in the desired order.

keySelector Func<T, TKey>

Identifies an item's identity across syncs.

itemComparer IEqualityComparer<T>

Decides whether an item at a matched key needs replacing. When null, Default is used.

Type Parameters

T

The element type, shared by both source and target.

TKey

The key type identifying an item's identity across syncs.

Exceptions

ArgumentException

source contains a duplicate key.

SyncFrom<TSource, T, TKey>(ObservableCollection<T>, IEnumerable<TSource>, Func<TSource, TKey>, Func<T, TKey>, Func<TSource, T>, Action<T, TSource>?)

Reconciles target to match source in place, projecting each TSource to a T row (e.g. a view model built from a model). An item whose key is no longer present is removed; a new key is inserted via create; an existing key is moved to its new position and, when update is supplied, refreshed in place via update(row, source) — every sync, regardless of whether the source actually changed, so update should assign through property setters that themselves no-op when the value is unchanged (as a BindableBase-style SetProperty does).

This overload never replaces a matched row — only create ever produces a new T instance. That is deliberate: some binding frameworks drop the selected row on a Replace, and a row's identity is exactly what a page view model wants to keep stable across a sync. When update is omitted, a matched row is left untouched.

Worst case is O(n²) moves — acceptable for UI-sized lists, not for large ones.

public static void SyncFrom<TSource, T, TKey>(this ObservableCollection<T> target, IEnumerable<TSource> source, Func<TSource, TKey> sourceKey, Func<T, TKey> targetKey, Func<TSource, T> create, Action<T, TSource>? update = null) where TKey : notnull

Parameters

target ObservableCollection<T>

The collection to reconcile in place.

source IEnumerable<TSource>

The desired contents, in the desired order.

sourceKey Func<TSource, TKey>

Identifies a source item's identity across syncs.

targetKey Func<T, TKey>

Identifies a row's identity across syncs.

create Func<TSource, T>

Builds a new row for a source item with no matching existing row.

update Action<T, TSource>

Refreshes an existing row from its matched source item. When null, matched rows are left untouched.

Type Parameters

TSource

The source element type (e.g. a model).

T

The target element type (e.g. a view model).

TKey

The key type identifying a row's identity across syncs.

Exceptions

ArgumentException

source contains a duplicate key.