ListMapping 1.0.1
dotnet add package ListMapping --version 1.0.1
NuGet\Install-Package ListMapping -Version 1.0.1
<PackageReference Include="ListMapping" Version="1.0.1" />
<PackageVersion Include="ListMapping" Version="1.0.1" />
<PackageReference Include="ListMapping" />
paket add ListMapping --version 1.0.1
#r "nuget: ListMapping, 1.0.1"
#:package ListMapping@1.0.1
#addin nuget:?package=ListMapping&version=1.0.1
#tool nuget:?package=ListMapping&version=1.0.1
📚 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:
- Verbose and Repetitive: The same mapping logic gets written repeatedly.
- Error-Prone: Manual handling increases the chance of bugs.
- 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
- Identify the ID property or selector used to match list items.
- Pass lists and property names (or lambda selectors) to
MapList
. - 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
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -am 'Add new feature'
). - Push the branch (
git push origin feature/your-feature
). - Create a pull request.
🧑💻 Author
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 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. |
-
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.