ListMapping 1.0.1

dotnet add package ListMapping --version 1.0.1
                    
NuGet\Install-Package ListMapping -Version 1.0.1
                    
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="ListMapping" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ListMapping" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ListMapping" />
                    
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 ListMapping --version 1.0.1
                    
#r "nuget: ListMapping, 1.0.1"
                    
#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 ListMapping@1.0.1
                    
#: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=ListMapping&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ListMapping&version=1.0.1
                    
Install as a Cake Tool

📚 ListMapping

ListMapping is a library designed to simplify and optimize the process of synchronizing two lists based on matching identifiers. Say goodbye to repetitive nested for loops when mapping lists!


🐞 The Problem

When mapping two lists in C#, developers often need to nested loops or repetitive code patterns:

foreach (var source in sourceList)
{
    var destination = destinationList.FirstOrDefault(d => d.Id == source.Id);
    if (destination != null)
    {
        destination.Name = source.Name;
    }
    else
    {
        destinationList.Add(new Destination { Id = source.Id, Name = source.Name });
    }
}

// Remove unmatched items
destinationList.RemoveAll(d => !sourceList.Any(s => s.Id == d.Id));

🚨 Issues with this approach:

  1. Verbose and Repetitive: The same mapping logic gets written repeatedly.
  2. Error-Prone: Manual handling increases the chance of bugs.
  3. Difficult to Maintain: Changes in mapping logic require updates in multiple places.

The Solution

ListMapping provides a cleaner and more reusable way to handle list mapping with just one line of code.

1. Mapping by Property Name

Map two lists using property named ("Id"):

using ListMapping;

destinationList.MapList(sourceList);

2. Mapping by Lambda Expressions

Map two lists using lambda expressions for better type safety:

using ListMapping;

destinationList.MapList(
    sourceList,
    src => src.Id,
    dest => dest.Id
);

3. Mapping by Property Name

Map two lists using property names:

using ListMapping;

destinationList.MapList(sourceList, "Id", "Id");

No more nested loops or manual mapping logic!


🛠️ Key Benefits

  • Clean Code: One line replaces multiple nested loops.
  • Reusable Logic: Abstracts the mapping logic for repeated use.
  • Performance Optimized: Cached property accessors for better runtime performance.
  • Flexible: Supports both property names and lambda expressions.

📊 Use Cases

  • Synchronizing data from external sources (e.g., API responses).
  • Mapping DTOs to domain models or vice versa.
  • Keeping two lists in sync in business workflows.

📦 Installation

Install via NuGet:

dotnet add package ListMapping --version 1.0.0

Or update your .csproj:

<PackageReference Include="ListMapping" Version="1.0.0" />

🧠 How It Works

  1. Identify the ID property or selector used to match list items.
  2. Pass lists and property names (or lambda selectors) to MapList.
  3. The library handles:
    • Adding missing items.
    • Updating existing items.
    • Removing unmatched items.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.


🤝 Contributing

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push the branch (git push origin feature/your-feature).
  5. Create a pull request.

🧑‍💻 Author

  • Osama
  • LinkedIn
  • GitHub
  • Stack Overflow
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 was computed.  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 was computed.  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.
  • net8.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
1.0.1 92 12/27/2024
1.0.0 88 12/27/2024