Update a collection with another collection in .NET
It can be a pain to update a collection using another collection. This library is designed to make this process easier, with a simple fluent API.
var destination = new List<Item>
{
new Item { Id = 1, Name = "A" },
new Item { Id = 2, Name = "B" }
};
var source = new List<Item>
{
new Item { Id = 1, Name = "B" },
new Item { Id = 3, Name = "C" }
};
destination = destination
.UsingSource(source)
.CompareWith((d, s) => d.Id == s.Id)
.CreateWith(s => new Item { Id = s.Id, Name = s.Name, Date = DateTime.Now })
.UpdateWith((d, s) => { d.Name = s.Name + " updated"; })
.WithDelete()
.Execute();
The above code would:
- Update the exiting item where
Id = 1
to haveName = "B updated"
- Create a new item where
Id = 3
andName = "C updated"
- Delete item where
Id = 2
which doesn't exist in the source collection
- The entry point for UpdateCollection.
- Call this extension method on an existing IEnumerable instance.
- (param: source) A source IEnumerable instance which is used to create/update/delete items in the destination. It does not have to be of the same type as the destination.
- To be chained to
.UsingSource()
- (param: func) A delegate that returns a boolean which is called for each item in the source collection as a comparator to use which looks for a match against an item in the destination collection.
- Optional method which can be chained to
.CompareWith()
- (param: func) A delegate that returns an instance of TDestination which is called for each item in the source collection that needs to be created in the destination. It provides the current source item which can be used to construct a destination item.
- Optional method which can be chained to
.CompareWith()
or.CreateWith()
- (param: func) A delegate that returns an instance of TDestination which is called for each item in the source collection that needs to be created in the destination. It provides the current source item which can be used to construct a destination item.
- You must create the TDestination item yourself as it is not created automatically.
- Optional method which can be chained to
.CreateWith()
or.UpdateWith()
- Remove any items from the destination which do not exist in the source, based on the delegate in
.CompareWith()
- Required. Must be called last and can be chained to
.CreateWith()
,.UpdateWith()
or.WithDelete()
- Returns the new and updated version of the destination collection