CollectionComparer 0.5.0.1
dotnet add package CollectionComparer --version 0.5.0.1
NuGet\Install-Package CollectionComparer -Version 0.5.0.1
<PackageReference Include="CollectionComparer" Version="0.5.0.1" />
paket add CollectionComparer --version 0.5.0.1
#r "nuget: CollectionComparer, 0.5.0.1"
// Install CollectionComparer as a Cake Addin
#addin nuget:?package=CollectionComparer&version=0.5.0.1
// Install CollectionComparer as a Cake Tool
#tool nuget:?package=CollectionComparer&version=0.5.0.1
Conway Collection Comparer
The Collection Comparer compares two collection and exposes on opprotunity to execute code depending if the item is in the source (left) collection, the comparing (right) collection or both collections.
For example, I have two collections. One collection is from the database, a second from the UI. In the UI the user removes some items and adds some new items. When the user saves the changes we need to reconcile the differences.
One possiblity is removing all the items and add them anew. The problem is if you have foregin keys, it quickly becomes challenging. An alternative is discovering the differences and update the database accordingly. This is where the Collection Comparer comes in.
An implementation might look like this:
private static void MergeAlbums(IEnumerable<int> existing, IEnumerable<int> newAlbums, PhotoSchema photo)
{
existing.CompareTo(newAlbums, (s, d) => s == d)
.OnlyInSourceCollection(s =>
{
// if only in the database collection, then it's been removed.
var album = photo.Albums.SingleOrDefault(x => x.AlbumId == s);
photo.Albums.Remove(album);
})
.OnlyInComparingCollection(s =>
{
// if it's only in the comparing collection, then it's new
// and needs to be added to the database.
photo.Albums.Add(new PhotoAlbumSchema
{
AlbumId = s,
PhotoId = photo.PhotoId
});
})
.Process();
}
Another example:
var source = new []{1, 3, 4, 6};
var collection = new[] {1, 2, 3, 4, 5};
source.CompareTo(collection, (s, d) => s == d)
.OnlyInSourceCollection(s=> {/* do something */})
.OnlyInComparingCollection(s=>{/* do something */})
.InBoth(s=> {/*do something*/})
.Process();
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 is compatible. |
.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. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETStandard 2.1
- 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.5.0.1 | 442 | 8/9/2020 |