Epiforge.Extensions.Expressions
4.2.0
See the version list below for details.
dotnet add package Epiforge.Extensions.Expressions --version 4.2.0
NuGet\Install-Package Epiforge.Extensions.Expressions -Version 4.2.0
<PackageReference Include="Epiforge.Extensions.Expressions" Version="4.2.0" />
<PackageVersion Include="Epiforge.Extensions.Expressions" Version="4.2.0" />
<PackageReference Include="Epiforge.Extensions.Expressions" />
paket add Epiforge.Extensions.Expressions --version 4.2.0
#r "nuget: Epiforge.Extensions.Expressions, 4.2.0"
#:package Epiforge.Extensions.Expressions@4.2.0
#addin nuget:?package=Epiforge.Extensions.Expressions&version=4.2.0
#tool nuget:?package=Epiforge.Extensions.Expressions&version=4.2.0
This library has useful tools for dealing with expressions:
ExpressionEqualityComparer- Defines methods to support the comparison of expression trees for equalityExpressionExtensions, providing:Duplicate- Duplicates the specified expression treeSubstituteMethods- Recursively scans an expression tree to replace invocations of specific methods with replacement methods
Observable
This library accepts a LambdaExpression and arguments to pass to it, dissects the LambdaExpression's body, and hooks into change notification events for properties (INotifyPropertyChanged), collections (INotifyCollectionChanged), and dictionaries (Epiforge.Extensions.Collections.INotifyDictionaryChanged).
// Employee implements INotifyPropertyChanged
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
// expr subscribed to elizabeth's PropertyChanged
Then, as changes involving any elements of the expression occur, a chain of automatic re-evaluation will get kicked off, possibly causing the observable expression's Evaluation property to change.
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
// expr.Evaluation.Result == 9
elizabeth.Name = "Lizzy";
// expr.Evaluation.Result == 5
Also, since exceptions may be encountered after an observable expression was created due to subsequent element changes, observable expressions include a Fault property in their evaluations, which will be set to the exception that was encountered during evaluation.
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
// expr.Evaluation.Fault is null
elizabeth.Name = null;
// expr.Evaluation.Fault is NullReferenceException
Observable expressions raise property change events of their own, so listen for those (kinda the whole point)!
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
var expr = observer.Observe(e => e.Name.Length, elizabeth);
expr.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "Evaluation")
{
var (fault, result) = expr.Evaluation;
if (fault is not null)
{
// Whoops
}
else
{
// Do something with result
}
}
};
While an expression is working out its new value it can pass through results that were never simultaneously true of its inputs; an addition whose two operands both derive from the same property has to compute one of them before the other. You are not told about those. Every event you receive carries a value the expression genuinely held, so a subscriber that redraws or broadcasts on one does that work once rather than twice, the second time only to correct the first.
Nor are you told anything at all when a change leaves the value where it found it. That is decided by a comparison, using the same equality the expression uses everywhere else, and it happens before PropertyChanging rather than after — so a handler for that event still reads the previous value, and a pair of events always means the value really moved.
When you dispose of your observable expression, it will disconnect from all the events.
var elizabeth = Employee.GetByName("Elizabeth");
var observer = new ExpressionObserver();
using (var expr = observer.Observe(e => e.Name.Length, elizabeth))
{
// expr subscribed to elizabeth's PropertyChanged
}
// expr unsubcribed from elizabeth's PropertyChanged
How an Expression Gets Observed
Observe takes a shortcut when it can and builds a graph when it cannot, deciding once when the observation is created. You receive the same values through the same events either way; the shortcut is just faster and lighter.
The shortcut handles an expression built from these:
- the argument, constants, and captured locals
- fields, on anything above — including static fields
- properties and indexers whose target is one of the above
- static properties
- operators, where one resolved to a method needs a return type nothing could dispose —
==on strings qualifies
Everything else builds the graph: ?:, &&, || and ??; anything read through a property, such as e => e.Name.Length; method calls; object and collection construction; and anything you have configured the observer to ignore notifications for or to dispose.
To find out about a particular expression, ask:
var analysis = new DirectSubscriptionAnalyzer(options).Analyze(expression.Body);
// analysis.IsEligible is false
// analysis.Ineligibility is DirectSubscriptionIneligibility.DeferredBranch
// analysis.IneligibleExpression is the part responsible
Hand the analyzer the same options you hand the observer, since some of them decide what gets subscribed to at all. Set UseDirectSubscription to false if you would rather always have the graph; it is true by default.
Fields Are Read Once
Whatever a field held when an observation began is what that observation goes on using — a captured local, a field of your own class, and a static field alike. Assigning it afterward does not reach an observation that already exists. Static properties behave the same way, so e => e.Hired < DateTime.Now compares against the moment it was created for as long as it lives.
var threshold = low;
using var expr = observer.Observe(e => e.Salary > threshold.Amount, elizabeth);
threshold = high; // expr is still comparing against low
low.Amount = 50000; // expr re-evaluates
high.Amount = 90000; // expr does not
If you want the comparison to follow the value, do not assign the field — make the thing it points at a property of an object that notifies, and read that instead.
Observable expressions will also try to automatically dispose of disposable objects they create in the course of their evaluation when and where it makes sense. Use the ExpressionObserverOptions class for more direct control over this behavior.
You can use the Optimizer property to specify an optimization method to invoke automatically during the observable expression creation process.
We recommend Tuomas Hietanen's Linq.Expression.Optimizer, the utilization of which would look like so:
var options = new ExpressionObserverOptions { Optimizer = ExpressionOptimizer.tryVisit };
var a = Expression.Parameter(typeof(bool));
var b = Expression.Parameter(typeof(bool));
var lambda = Expression.Lambda<Func<bool, bool, bool>>
(
Expression.AndAlso
(
Expression.Not(a),
Expression.Not(b)
),
a,
b
); // lambda explicitly defined as (a, b) => !a && !b
var observer = new ExpressionObserver(options);
var expr = observer.Observe<bool>(lambda, false, false);
// optimizer has intervened and defined expr as (a, b) => !(a || b)
// (because Augustus De Morgan said they're essentially the same thing, but this involves less steps)
Observable Queries
This library provides re-implementations of LINQ operations, but instead of returning IEnumerable<T>s and simple values, these return IObservableCollectionQuery<T>s, IObservableDictionaryQuery<TKey, TValue>s, and IObservableScalarQuery<T>s.
This is because, unlike traditional LINQ operations, these implementations continuously update their results until those results are disposed.
What they hand back is a read-only view of the source: change the source, and the query brings itself up to date. Queries do not implement the mutating range collection and dictionary interfaces, because a query result is not somewhere you put things.
But... what could cause those updates?
- the source is enumerable, implements
INotifyCollectionChanged, and raises aCollectionChangedevent - the source is a dictionary, implements
Epiforge.Extensions.Collections.INotifyDictionaryChanged<TKey, TValue>, and raises aDictionaryChangedevent - the elements in the enumerable (or the values in the dictionary) implement
INotifyPropertyChangedand raise aPropertyChangedevent - a reference enclosed by a selector or a predicate passed to the method implements
INotifyCollectionChanged,Epiforge.Extensions.Collections.INotifyDictionaryChanged<TKey, TValue>, orINotifyPropertyChangedand raises one of their events
That last one might be a little surprising, but this is because all selectors and predicates passed to Observable Query methods become Observable Expressions (see above).
This means that you will not be able to pass one that an ExpressionObserver cannot observe (e.g. a lambda expression that can't be converted to an expression tree or that contains nodes that are unsupported).
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 collectionObserver = new CollectionObserver();
var observedNotes = collectionObserver.ObserveReadOnlyList(notes);
var orderedNotes = observedNotes.ObserveOrderBy(note => note.LastEdited, isDescending: true);
notesViewControl.ItemsSource = orderedNotes;
From then on, as you add Notes to the notes observable collection, the IObservableCollectionQuery<Note> named orderedNotes will be kept ordered so that notesViewControl displays them in the preferred order.
Since IObservableCollectionQuery<T>'s are automatically subscribing to events for you, you do need to call Dispose on them when you don't need them any more.
void Page_Unload(object? sender, EventArgs e)
{
orderedNotes.Dispose();
observedNotes.Dispose();
}
Ahh, but what about exceptions?
Well, Observable Expressions contain a Fault element in their Evaluation properties, but... you don't really see those Observable Expressions as an Observable Query caller, do ya?
For that reason, Observable Queries all have OperationFault properties.
You may subscribe to their PropertyChanging and PropertyChanged events to be notified when an Observable Expression or the overall Observable Query runs into a problem.
If there is more than one fault in play, the value of OperationFault will be an AggregateException.
Dictionary queries adopt the key comparer of the dictionary they observe, discovering it through Epiforge.Extensions.Collections.Generic.IHashKeys<TKey> or a Dictionary<TKey, TValue>'s own Comparer, so a query over a case-insensitive dictionary is itself case-insensitive.
ObserveGroupBy, ObserveToLookup, and ObserveDistinct do not order their results the way LINQ does.
Groupings are ordered by when they were created and the elements of a grouping by when they were added, rather than by where they occur in the source.
This is deliberate: holding a grouping at the position of its key's first occurrence would mean moving that grouping every time an element was inserted ahead of it, announcing a change to something whose membership did not change, which is the opposite of what an Observable Query is for.
Call ObserveOrderBy on the query, or on a grouping, when you want a defined order.
Reach for foreach rather than the indexer, because the difference between them is larger than it looks and grows with the collection.
An enumeration takes the query's lock once and then walks a list, while the indexer takes that lock again for every element you ask for; on a large collection it must also find each one in a tree, because a query keeps its elements' positions in one so that a change repairs only what it touched.
A query does remember the position it handed out last and searches outward from there, so asking for positions in order, or near one another, costs a fraction of asking for them at random, and what remains is mostly the repeated locking rather than the search.
Walking ten thousand elements by index instead of by enumerator measured between thirty and fifty times slower in order, and around two hundred times out of order; at a hundred elements it was about fifteen, and there the repeated locking is the whole of it.
Where you do need elements by position more than once, copy the query's contents and index the copy.
Since the ExpressionObserver has a number of options governing its behavior, you may optionally pass one you've made to the constructor of CollectionObserver to ensure those options are obeyed when Observable Expressions are created to enable your Observable Queries.
How Observable Queries Work and When to Use Them
It is worth being plain about what kind of thing this is, because "LINQ, but observable" undersells it and sets the wrong expectations.
A LINQ query is a description of a computation you run. Run it again and it does all of the work again. An Observable Query is not re-run. It is a small machine that holds the answer and repairs it, so when something changes, only the parts of the answer that depended on that thing are recomputed. The work is proportional to what changed rather than to how much data you have. If you want the name the literature uses for this idea, it is incremental, or self-adjusting, computation.
Three things that might otherwise look like arbitrary restrictions fall straight out of that:
- Your selectors and predicates have to be expression trees rather than delegates because the machine has to read them to find out what they depend on. A delegate is opaque; there is nothing in it to subscribe to.
- You have to dispose of a query because it is holding subscriptions to everything it depends on, and those subscriptions are the entire reason the answer stays right.
- Faults reach you through
OperationFaultinstead of being thrown, because the evaluation that failed happened later, on whatever thread raised the change. By then there is no call of yours left on the stack to throw out of.
What is not free is construction. Building the machine means building an observable expression for every element the query touches, and that is proportional to the size of the collection. So build a query once and hold onto it. Do not build one per frame, per request, or per keystroke. The bargain is that you pay up front and then stop paying to read.
Which is also how to decide whether you want one. If you compute a result once and move on, plain LINQ is cheaper and simpler, and you should use it. If a result has to stay correct across a long run of small changes, such as a list someone is looking at, a running total, or a filter someone is typing into, that is what these are for.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 is compatible. 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 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Epiforge.Extensions.Collections (>= 4.0.0)
- Epiforge.Extensions.Components (>= 4.1.1)
-
net6.0
- Epiforge.Extensions.Collections (>= 4.0.0)
- Epiforge.Extensions.Components (>= 4.1.1)
- System.Collections.Immutable (>= 8.0.0)
-
net7.0
- Epiforge.Extensions.Collections (>= 4.0.0)
- Epiforge.Extensions.Components (>= 4.1.1)
- System.Collections.Immutable (>= 8.0.0)
-
net8.0
- Epiforge.Extensions.Collections (>= 4.0.0)
- Epiforge.Extensions.Components (>= 4.1.1)
-
net9.0
- Epiforge.Extensions.Collections (>= 4.0.0)
- Epiforge.Extensions.Components (>= 4.1.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 |
|---|---|---|
| 4.3.0 | 0 | 9/2/2026 |
| 4.2.0 | 42 | 8/31/2026 |
| 4.1.0 | 41 | 8/31/2026 |
| 4.0.0 | 95 | 8/30/2026 |
| 3.0.1 | 81 | 8/29/2026 |
| 3.0.0 | 170 | 8/27/2026 |
| 2.3.8 | 217 | 6/6/2026 |
| 2.3.7 | 119 | 6/6/2026 |
| 2.3.6 | 125 | 6/6/2026 |
| 2.3.5 | 117 | 6/3/2026 |
| 2.3.4 | 122 | 5/31/2026 |
| 2.3.3 | 118 | 5/29/2026 |
| 2.3.2 | 129 | 5/8/2026 |
| 2.3.1 | 119 | 5/7/2026 |
| 2.3.0 | 119 | 5/7/2026 |
| 2.2.0 | 129 | 4/23/2026 |
| 2.1.2 | 129 | 4/14/2026 |
| 2.1.1 | 139 | 4/12/2026 |
| 2.1.0 | 124 | 4/5/2026 |
| 2.0.2 | 255 | 12/24/2025 |
An expression which reads through a field is now eligible for direct subscription, where before only a field of a compiler-generated closure type was. A field raises no change notification whoever declares it, so both mechanisms read one once when the observation begins and hold it; refusing a field of an ordinary object was conservative rather than necessary. This admits the shape most real predicates take — a threshold held on a view model or a setting held on a service — and a field of the argument, which is how a tuple argument's elements are reached. An observable query whose predicate is of that shape now constructs in between a quarter and a fifth of the time on a third of the memory, measured across ten thousand elements.
An optimizer supplied through ExpressionObserverOptions is now invoked at most once per expression instance rather than once per use. The observable query methods optimize their selector to compute a cache key, so constructing a query repeatedly over a selector you hold paid a complete optimization pass to look up an entry it already had; that lookup is now about thirty times faster and allocates nothing beyond what it allocates with no optimizer configured. Observing an expression with an optimizer configured is a little over twice as fast for the same reason. The Optimizer property of ExpressionObserver is consequently no longer reference equal to the method supplied through the options, being that method wrapped; it behaves identically otherwise.
The diagram by which an expression is compared for structural equality is now built in a buffer reused per thread and copied out at exactly the size needed, where before it accumulated in a list which grew by doubling and was handed out oversized. Observing an expression through the graph consequently allocates about five hundred and eighty fewer bytes, near a fifth less than before, and constructs between fourteen and seventeen percent faster; the diagrams themselves are unchanged.
An observation which evaluates directly no longer allocates when a change to one of its sources leaves its value what it already was. Storing the result of an expression of a value type boxed it, and the boxing happened before the comparison which decides whether anything actually changed, so every expression watching a shared value allocated a box on every notification and discarded it again. Where a shared value changes and most of the expressions watching it keep their answer — a threshold moving against a thousand comparisons — propagation now allocates about a ninetieth of what it did and takes two thirds of the time. An expression whose value does change still allocates exactly what it did before.
An operator which the compiler resolves to a method is now eligible for direct subscription when its return type cannot implement either disposal interface, being sealed or a value type and implementing neither. This admits the comparison of two strings, which is what person.Name == "Emily" compiles to, along with the arithmetic and comparison operators of decimal, DateTime, TimeSpan, Guid and every other sealed or value type whose operators return one. Such an operator was refused because the graph registers the value a method returns for disposal, which the fast path does not do; where the return type cannot be disposed, what the graph performs is three type tests which cannot succeed, so there was nothing to preserve. An observable query whose predicate compares two strings now constructs about six and a half times faster on rather less than half the memory, measured across a thousand elements, which is what the same query costs comparing two integers.
A static field and a static property are now fixed targets, so an expression which reads through one is eligible for direct subscription where before it was not. Neither can announce that it has changed, and the graph accordingly reads each once when an observation begins and holds it for the life of that observation; the fast path now does the same, freezing the value rather than reading it afresh. This admits a setting reached through a static, and it admits a static property whose type cannot implement a disposal interface, which is what made DateTime.Now ineligible. An observable query whose predicate reads through a static now constructs between six and a half and seven and a half times faster on rather less than half the memory, measured across a thousand elements.
AddMethodReturnValueDisposal, AddPropertyValueDisposal, AddExpressionValueDisposal and AddConstructedTypeDisposal now refuse a registration whose type could never implement a disposal interface, being sealed or a value type and implementing neither, and return false rather than recording it. Such a registration never had any effect: the expression observer disposes a value by testing it for those interfaces at runtime, and for such a type that test cannot succeed. Nothing which worked before behaves differently; a call which was already doing nothing now says so.
An observation which evaluates directly no longer allocates an array to hold nothing. It built one array for the values it freezes when the observation begins and another for its subscriptions, without checking whether either was empty, so an expression which freezes nothing paid twenty-four bytes for an empty array and an expression which subscribes to nothing paid twenty-four more. A predicate which reads a property of the element it is given freezes nothing, which is to say this was paid by the most ordinary shape there is: an observable query over such a predicate now allocates twenty-four fewer bytes per element, and one which does not mention the element at all, forty-eight fewer.
This package now depends on version 4.0.0 of Epiforge.Extensions.Collections. Nothing in this package's own interface has changed, but the notifications an observable query relays from an ObservableRangeCollection have: a range operation which replaces one number of items with a different number is now described as a replacement of what both sides have plus an addition or removal of the surplus, removing items which happen to be adjacent is described by one event rather than by one for each, and the properties a range operation announces are announced before the event describing it rather than after.