CollectionMerger 0.1.5
dotnet add package CollectionMerger --version 0.1.5
NuGet\Install-Package CollectionMerger -Version 0.1.5
<PackageReference Include="CollectionMerger" Version="0.1.5" />
<PackageVersion Include="CollectionMerger" Version="0.1.5" />
<PackageReference Include="CollectionMerger" />
paket add CollectionMerger --version 0.1.5
#r "nuget: CollectionMerger, 0.1.5"
#:package CollectionMerger@0.1.5
#addin nuget:?package=CollectionMerger&version=0.1.5
#tool nuget:?package=CollectionMerger&version=0.1.5
CollectionMerger
Synchronize/merge collections while generating a change report (added/updated/removed), including nested collection merges.
- Targets:
net8.0,net9.0,net10.0 - Usage: Call
.MapFrom(...)or.MapFromAsync(...)on any collection, passing another collection to merge from - Output:
SyncReportwith a list ofChangeRecorditems
See CHANGELOG.md for release history.
Installation
.NET CLI
dotnet add package CollectionMerger
Package Manager
Install-Package CollectionMerger
PackageReference
<PackageReference Include="CollectionMerger" Version="x.y.z" />
Getting started
You merge a source collection into a destination collection by providing:
matchPredicate: how to match a source item to an existing destination item (usually by an ID)mapProperties: how to copy/update properties from source to destinationisSourceDeleted(optional): treat a source item as deleted even when it exists in the source collectiondeleteDestination(optional): custom delete action for destination items (default removes from the collection)
Synchronous version
using CollectionMerger;
var destination = new List<Person>
{
new() { Id = 1, Name = "Alice" },
new() { Id = 2, Name = "Bob" }
};
var source = new List<PersonDto>
{
new() { Id = 1, Name = "Alice Updated" },
new() { Id = 3, Name = "Charlie" }
};
var report = destination.MapFrom(
source: source,
matchPredicate: (src, dest) => src.Id == dest.Id,
mapProperties: (src, dest, _m) =>
{
dest.Id = src.Id;
dest.Name = src.Name;
});
Console.WriteLine($"Added: {report.AddedCount}, Updated: {report.UpdatedCount}, Removed: {report.RemovedCount}");
Async version
For async scenarios (e.g., fetching related data from a database, calling APIs), use MapFromAsync:
using CollectionMerger;
var report = await destination.MapFromAsync(
source: source,
matchPredicate: async (src, dest) =>
{
// Can perform async operations here
return src.Id == dest.Id;
},
mapProperties: async (src, dest, _m) =>
{
dest.Id = src.Id;
dest.Name = await FetchUpdatedNameAsync(src.Id); // Example async operation
});
Console.WriteLine($"Added: {report.AddedCount}, Updated: {report.UpdatedCount}, Removed: {report.RemovedCount}");
Examples
Nested collections (people + cats)
For nested collections, call MapFrom(...) on the child collection and pass the parent Mapper so paths get nested.
using CollectionMerger;
var report = destinationPeople.MapFrom(
source: sourcePeople,
matchPredicate: (srcPerson, destPerson) => srcPerson.ID == destPerson.ID,
mapProperties: (srcPerson, destPerson, m1) =>
{
destPerson.ID = srcPerson.ID;
destPerson.Name = srcPerson.Name;
destPerson.Cats.MapFrom(
parent: m1,
source: srcPerson.Cats,
matchPredicate: (srcCat, destCat) => srcCat.ID == destCat.ID,
mapProperties: (srcCat, destCat, _m2) =>
{
destCat.ID = srcCat.ID;
destCat.Name = srcCat.Name;
});
});
Async nested collections
For async nested collections, use MapFromAsync:
using CollectionMerger;
var report = await destinationPeople.MapFromAsync(
source: sourcePeople,
matchPredicate: async (srcPerson, destPerson) => srcPerson.ID == destPerson.ID,
mapProperties: async (srcPerson, destPerson, m1) =>
{
destPerson.ID = srcPerson.ID;
destPerson.Name = srcPerson.Name;
await destPerson.Cats.MapFromAsync(
parent: m1,
source: srcPerson.Cats,
matchPredicate: async (srcCat, destCat) => srcCat.ID == destCat.ID,
mapProperties: async (srcCat, destCat, _m2) =>
{
destCat.ID = srcCat.ID;
destCat.Name = await FetchCatNameAsync(srcCat.ID); // Example async operation
});
});
Inspecting changes
foreach (var change in report.Changes)
{
Console.WriteLine($"{change.ChangeType}: {change.Path}");
if (change.PropertyChanges is null)
continue;
foreach (var prop in change.PropertyChanges)
Console.WriteLine($" - {prop.PropertyName}: '{prop.OldValue}' -> '{prop.NewValue}'");
}
Source-driven deletes + custom delete actions
Mark a source item as deleted and remove its destination match (default removal):
var report = destination.MapFrom(
source: source,
matchPredicate: (src, dest) => src.ID == dest.ID,
mapProperties: (src, dest, _m) =>
{
dest.ID = src.ID;
dest.Name = src.Name;
},
isSourceDeleted: src => src.Deleted);
Provide a custom delete action (soft delete instead of removing):
var report = destination.MapFrom(
source: source,
matchPredicate: (src, dest) => src.ID == dest.ID,
mapProperties: (src, dest, _m) =>
{
dest.ID = src.ID;
dest.Name = src.Name;
},
deleteDestination: dest => dest.Deleted = true);
What the report contains
SyncReport.Changes contains ChangeRecord entries:
ChangeType:Added,Updated, orRemovedPath: a stable-ish path for the item (supports nesting)Item: the destination item instancePropertyChanges: only present forUpdated
FAQ
How are item paths created?
Paths look like Person[1] and Person[1].Cat[3].
The [...] value is chosen by looking for a public readable ID or Id property on either the source or destination item. If neither exists, it becomes ?.
What counts as an update?
After your mapProperties delegate runs, CollectionMerger snapshots public instance scalar properties (excluding enumerables except string) and records an Updated change if any of those values differ.
Are collections compared automatically?
No. Collection properties are ignored for property change detection.
If you want nested changes, perform nested merges with the nested overload of MapFrom(...).
What are the requirements?
- Destination must be an
ICollection<TDestination> TDestinationmust have a parameterless constructor (new()constraint)- Matching behavior is entirely defined by your
matchPredicate(make sure it uniquely identifies items)
Similar projects
AutoMapper.Collection
CollectionMerger is similar to AutoMapper.Collection.
Key differences:
- No Entity Framework dependency: CollectionMerger does not have a dependency on Entity Framework, but Entity Framework can be used with it.
- Soft deletes: Supports soft deletes, or even not deleting at all.
- Async/await: Supports async/await patterns.
- Low-level approach: Feels more low level than AutoMapper.Collection, giving you more control.
- No external dependencies: Has no external dependencies.
Feedback / issues
If you hit a bug or want to request a feature, please open an issue: https://github.com/alexdresko/collection-merger/issues
Development (this repo)
Releasing
Releases are automated via Release Please.
- Merge changes into
mainusing Conventional Commits. - Release Please will open/maintain a release PR updating
CHANGELOG.mdand package version. - Merging the release PR creates the GitHub Release + publishes to NuGet.
GitHub setup
The release workflow expects a GitHub Actions secret:
NUGET_API_KEY(orNUGET_TOKEN): a NuGet.org API key with permission to push packages.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
## [0.1.5](https://github.com/alexdresko/collection-merger/compare/v0.1.4...v0.1.5) (2026-01-06)
### Features
* add async equivalents for MapFrom methods ([8242643](https://github.com/alexdresko/collection-merger/commit/8242643ff36d1fbd11a50a62f321ed0590e971c0))
* add async equivalents for MapFrom methods ([#17](https://github.com/alexdresko/collection-merger/issues/17)) ([24c2af1](https://github.com/alexdresko/collection-merger/commit/24c2af17c00a84ee17434413f35817b7f5c3200c))
* add support for custom deletion behavior ([cd47243](https://github.com/alexdresko/collection-merger/commit/cd47243959cf48d687fb2e56570e9985ac0b0435))
* implement MapFromAsync with tests and documentation ([5b91586](https://github.com/alexdresko/collection-merger/commit/5b9158638cd9824dfa6ec2008726842ece3d77f2))
### Bug Fixes
* add --no-restore flag to build step in CI workflow ([00c2ffb](https://github.com/alexdresko/collection-merger/commit/00c2ffb9ec806c7716ec959547c071ff15c4b59f))
* add clean step to CI workflow to prevent cached build issues ([257d148](https://github.com/alexdresko/collection-merger/commit/257d148fd3ae8ffed9a0c70b3a37e7bac0a382ce))
* apply dotnet format to fix whitespace issues ([cf3ff14](https://github.com/alexdresko/collection-merger/commit/cf3ff14ed3f1f4e6af92ef089e88110ec1a52bc4))
* improve async performance and fix test typo ([f52eed8](https://github.com/alexdresko/collection-merger/commit/f52eed89e1a47b9c8bea4ac5f5dc60310c2f02b0))
* make test model classes public to fix compilation errors ([9a3d3ce](https://github.com/alexdresko/collection-merger/commit/9a3d3ce69c6a2522546f4e925001565505f18aea))
* refactor async helpers for better readability ([d201913](https://github.com/alexdresko/collection-merger/commit/d2019139204089f68279d5386862b7df2dd1a88f))
* remove placeholder files causing test compilation errors ([88aad5b](https://github.com/alexdresko/collection-merger/commit/88aad5b9ec0e10be6807684e37f47c703d276678))
* remove remaining placeholder files causing compilation errors ([7a5d74c](https://github.com/alexdresko/collection-merger/commit/7a5d74c2660c2ac9adf21345eed46322a603a215))
* simplify CI workflow to fix build failures ([2fe31b8](https://github.com/alexdresko/collection-merger/commit/2fe31b818121b9de576f56375ff811fbcb545a07))
* **tests:** update namespace import for AsyncTests ([f4976f8](https://github.com/alexdresko/collection-merger/commit/f4976f87bf717e11e8f2c3f92c045376f39472c8))