Cogs.ActiveQuery
1.8.0
dotnet add package Cogs.ActiveQuery --version 1.8.0
NuGet\Install-Package Cogs.ActiveQuery -Version 1.8.0
<PackageReference Include="Cogs.ActiveQuery" Version="1.8.0" />
paket add Cogs.ActiveQuery --version 1.8.0
#r "nuget: Cogs.ActiveQuery, 1.8.0"
// Install Cogs.ActiveQuery as a Cake Addin #addin nuget:?package=Cogs.ActiveQuery&version=1.8.0 // Install Cogs.ActiveQuery as a Cake Tool #tool nuget:?package=Cogs.ActiveQuery&version=1.8.0
This library provides re-implementations of extension methods you know and love from System.Linq.Enumerable
, but instead of returning Enumerable<T>
s and simple values, these return ActiveEnumerable<T>
s, ActiveDictionary<TKey, TValue>
s, and ActiveValue<T>
s. This is because, unlike traditional LINQ extension methods, these extension methods continuously update their results until those results are disposed.
But... what could cause those updates?
- the source is enumerable, implements
INotifyCollectionChanged
, and raises aCollectionChanged
event - the source is a dictionary, implements
Cogs.Collections.INotifyDictionaryChanged<TKey, TValue>
, and raises aDictionaryChanged
event - the elements in the enumerable (or the values in the dictionary) implement
INotifyPropertyChanged
and raise aPropertyChanged
event - a reference enclosed by a selector or a predicate passed to the extension method implements
INotifyCollectionChanged
,Cogs.Collections.INotifyDictionaryChanged<TKey, TValue>
, orINotifyPropertyChanged
and raises one of their events
That last one might be a little surprising, but this is because all selectors and predicates passed to Active Query extension methods become active expressions (see above). This means that you will not be able to pass one that the Active Expressions library doesn't support (e.g. a lambda expression that can't be converted to an expression tree or that contains nodes that Active Expressions doesn't deal with). But, in exchange for this, you get all kinds of notification plumbing that's just handled for you behind the scenes.
Suppose, for example, you're working on an app that displays a list of notes and you want the notes to be shown in descending order of when they were last edited.
var notes = new ObservableCollection<Note>();
var orderedNotes = notes.ActiveOrderBy(note => note.LastEdited, isDescending: true);
notesViewControl.ItemsSource = orderedNotes;
From then on, as you add Note
s to the notes
observable collection, the ActiveEnumerable<Note>
named orderedNotes
will be kept ordered so that notesViewControl
displays them in the preferred order.
Since the ActiveEnumerable<T>
is automatically subscribing to events for you, you do need to call Dispose
on it when you don't need it any more.
void Page_Unload(object sender, EventArgs e)
{
orderedNotes.Dispose();
}
But, you may ask, what happens if things are a little bit more complicated because of background work? Suppose...
SynchronizedObservableCollection<Note> notes;
ActiveEnumerable<Note> orderedNotes;
Task.Run(() =>
{
notes = new SynchronizedObservableCollection<Note>();
orderedNotes = notes.ActiveOrderBy(note => note.LastEdited, isDescending: true);
});
Since we called the Cogs.Collections.Synchronized.SynchronizedObservableCollection
constructor in the context of a TPL Task
and without specifying a SynchronizationContext
, operations performed on it will not be in the context of our UI thread. Manipulating this collection on a background thread might be desirable, but there will be a big problem if we bind a UI control to it, since non-UI threads shouldn't be messing with UI controls. For this specific reason, Active Query offers a special extension method that will perform the final operations on an enumerable (or dictionary) using a specific SynchronizationContext
.
var uiContext = SynchronizationContext.Current;
SynchronizedObservableCollection<Note> notes;
ActiveEnumerable<Note> orderedNotes;
ActiveEnumerable<Note> notesForBinding;
Task.Run(() =>
{
notes = new SynchronizedObservableCollection<Note>();
orderedNotes = notes.ActiveOrderBy(note => note.LastEdited, isDescending: true);
notesForBinding = orderedNotes.SwitchContext(uiContext);
});
Or, if you call SwitchContext
without any arguments but when you know you're already running in the UI's context, it will assume you want to switch to that.
SynchronizedObservableCollection<Note> notes;
ActiveEnumerable<Note> orderedNotes;
await Task.Run(() =>
{
notes = new SynchronizedObservableCollection<Note>();
orderedNotes = notes.ActiveOrderBy(note => note.LastEdited, isDescending: true);
});
var notesForBinding = orderedNotes.SwitchContext();
But, keep in mind that no Active Query extension methods mutate the objects for which they are called, which means now you have two things to dispose, and in the right order!
void Page_Unload(object sender, EventArgs e)
{
notesForBinding.Dispose();
orderedNotes.Dispose();
}
Ahh, but what about exceptions? Well, active expressions expose a Fault
property and raise PropertyChanging
and PropertyChanged
events for it, but... you don't really see those active expressions as an Active Query caller, do ya? For that reason, Active Query introduces the INotifyElementFaultChanges
interface, which is implemented by ActiveEnumerable<T>
, ActiveDictionary<TKey, TValue>
, and ActiveValue<T>
. You may subscribe to its ElementFaultChanging
and ElementFaultChanged
events to be notified when an active expression runs into a problem. You may also call the GetElementFaults
method at any time to retrieve a list of the elements (or key/value pairs) that have active expressions that are currently faulted and what the exception was in each case.
As with the Active Expressions library, you can use the static property Optimizer
to specify an optimization method to invoke automatically during the active expression creation process. However, please note that Active Query also has its own version of this property on the ActiveQueryOptions
static class. If you are not using Active Expressions directly, we recommend using Active Query's property instead because the optimizer will be called only once per extension method call in that case, no matter how many elements or key/value pairs are processed by it. Optimize your optimization, yo.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Cogs.ActiveExpressions (>= 1.17.2)
- Cogs.Collections (>= 1.12.1)
- Cogs.Collections.Synchronized (>= 1.7.0)
- Cogs.Components (>= 1.2.0)
- Cogs.Disposal (>= 1.6.0)
- Cogs.Reflection (>= 1.6.0)
- Cogs.Threading (>= 1.12.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.8.0 | 320 | 2/10/2023 |
1.7.0 | 282 | 1/26/2023 |
1.6.9 | 310 | 1/21/2023 |
1.6.8 | 453 | 7/2/2022 |
1.6.7 | 451 | 6/25/2022 |
1.6.6 | 448 | 6/25/2022 |
1.6.5 | 408 | 6/16/2022 |
1.6.4 | 413 | 6/4/2022 |
1.6.3 | 428 | 5/31/2022 |
1.6.2 | 431 | 5/11/2022 |
1.6.1 | 423 | 5/11/2022 |
1.6.0 | 436 | 5/11/2022 |
1.5.13 | 470 | 4/13/2022 |
1.5.12 | 457 | 4/13/2022 |
1.5.11 | 425 | 4/12/2022 |
1.5.10 | 450 | 4/4/2022 |
1.5.9 | 436 | 4/3/2022 |
1.5.7 | 438 | 3/30/2022 |
1.5.6 | 437 | 3/29/2022 |
1.5.5 | 436 | 3/29/2022 |
1.5.4 | 436 | 3/28/2022 |
1.5.2 | 452 | 3/28/2022 |
1.5.1 | 408 | 3/27/2022 |
1.5.0 | 300 | 1/5/2022 |
1.4.9 | 320 | 12/21/2021 |
1.4.8 | 311 | 12/21/2021 |
1.4.7 | 318 | 12/16/2021 |
1.4.6 | 306 | 12/16/2021 |
1.4.5 | 311 | 12/12/2021 |
1.4.4 | 355 | 11/3/2021 |
1.4.3 | 379 | 11/3/2021 |
1.4.2 | 336 | 10/25/2021 |
1.4.1 | 426 | 10/17/2021 |
1.4.0 | 387 | 7/15/2021 |
1.3.11 | 383 | 3/3/2021 |
1.3.10 | 363 | 2/27/2021 |
1.3.9 | 377 | 2/26/2021 |
1.3.8 | 352 | 2/20/2021 |
1.3.7 | 402 | 2/10/2021 |
1.3.6 | 352 | 2/8/2021 |
1.3.5 | 376 | 2/1/2021 |
1.3.4 | 338 | 1/30/2021 |
1.3.3 | 432 | 12/14/2020 |
1.3.2 | 499 | 10/24/2020 |
1.3.1 | 488 | 10/23/2020 |
1.3.0 | 546 | 10/23/2020 |
1.2.2 | 444 | 10/22/2020 |
1.2.1 | 480 | 10/22/2020 |
1.2.0 | 457 | 10/20/2020 |
1.1.1 | 508 | 10/19/2020 |
1.1.0 | 453 | 10/4/2020 |
1.0.22 | 490 | 9/28/2020 |
1.0.21 | 612 | 9/27/2020 |
1.0.20 | 566 | 9/27/2020 |
1.0.19 | 575 | 9/27/2020 |
1.0.18 | 479 | 6/2/2020 |
1.0.17 | 485 | 5/27/2020 |
1.0.16 | 449 | 5/26/2020 |
1.0.15 | 481 | 5/26/2020 |
1.0.14 | 550 | 5/17/2020 |
1.0.13 | 482 | 5/15/2020 |
1.0.12 | 477 | 5/13/2020 |
1.0.11 | 484 | 5/13/2020 |
1.0.10 | 519 | 5/13/2020 |
1.0.9 | 472 | 5/12/2020 |
1.0.8 | 516 | 5/9/2020 |
1.0.7 | 521 | 5/9/2020 |
1.0.6 | 487 | 5/7/2020 |
1.0.5 | 498 | 5/7/2020 |
1.0.4 | 530 | 4/29/2020 |
1.0.3 | 481 | 4/17/2020 |
1.0.2 | 514 | 4/17/2020 |
1.0.1 | 539 | 3/4/2020 |
1.0.0 | 554 | 3/4/2020 |
ActiveGroupBy now returns an IActiveEnumerable of IActiveGrouping instances. ActiveSelect, ActiveSelectMany, and ActiveWhere have been reverted not to use disposable values caches. We may implement a caching alternative later.