CollectionMerger 0.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package CollectionMerger --version 0.1.2
                    
NuGet\Install-Package CollectionMerger -Version 0.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CollectionMerger" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CollectionMerger" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="CollectionMerger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CollectionMerger --version 0.1.2
                    
#r "nuget: CollectionMerger, 0.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CollectionMerger@0.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CollectionMerger&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=CollectionMerger&version=0.1.2
                    
Install as a Cake Tool

CollectionMerger

NuGet NuGet Downloads CI

Synchronize/merge collections while generating a change report (added/updated/removed), including nested collection merges.

  • Targets: net8.0, net9.0, net10.0
  • Main entry point: CollectionSyncExtensions.MapFrom(...)
  • Output: SyncReport with a list of ChangeRecord items

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 destination
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}");

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;
            });
    });

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}'");
}

What the report contains

SyncReport.Changes contains ChangeRecord entries:

  • ChangeType: Added, Updated, or Removed
  • Path: a stable-ish path for the item (supports nesting)
  • Item: the destination item instance
  • PropertyChanges: only present for Updated

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>
  • TDestination must have a parameterless constructor (new() constraint)
  • Matching behavior is entirely defined by your matchPredicate (make sure it uniquely identifies items)

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

This repo publishes to NuGet when you push a tag like v1.2.3.

  • Bump version + tag + push (PowerShell):
    • ./scripts/Release-Version.ps1 -Version 1.2.3

GitHub setup

The release workflow expects a GitHub Actions secret:

  • NUGET_API_KEY: a NuGet.org API key with permission to push packages.
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
0.1.5 97 1/6/2026
0.1.4 101 1/6/2026
0.1.2 98 1/6/2026
0.1.1 100 1/5/2026