I recently found the need to Sync two lists. I have one list that is used for display, and I want to dynamically sync that list with a new one by applying a delta.
I thought that this would be difficult, but I was surprised at its ease.
1:
2: Module SyncExtensions
3:
4: <System.Runtime.CompilerServices.Extension()> _
5: Public Sub Sync(Of TItem)(ByVal targetItems As ICollection(Of TItem), ByVal sourceItems As IEnumerable(Of TItem))
6: Dim o As Object = DirectCast(targetItems, ICollection).SyncRoot
7: If Monitor.TryEnter(o, TimeSpan.FromSeconds(10)) Then
8: ' Find items in source that are not in target
9: Dim itemsToAdd As New Collection(Of TItem)
10: For Each Item In sourceItems
11: If Not targetItems.Contains(Item) Then
12: itemsToAdd.Add(Item)
13: End If
14: Next
15: ' Apply all adds
16: For Each Item In itemsToAdd
17: targetItems.Add(Item)
18: Next
19: ' Find tags in target that should not be in source
20: Dim itemsToRemove As New Collection(Of TItem)
21: For Each Item In targetItems
22: If Not sourceItems.Contains(Item) Then
23: itemsToRemove.Add(Item)
24: End If
25: Next
26: ' Apply all removes
27: For Each Item In itemsToRemove
28: targetItems.Remove(Item)
29: Next
30: ' Dispose Timer
31: Monitor.Exit(o)
32: End If
33: End Sub
34:
35: End Module
You need to remember to lock the object while you sync. This is to allow your threading to take place without incident. The nitty gritty is just a case of comparing the two lists and building a list of changes to make and then removing them :)
Technorati Tags: .NET
Smart Classifications
Each classification [Concepts, Categories, & Tags] was assigned using AI-powered semantic analysis and scored across relevance, depth, and alignment. Final decisions? Still human. Always traceable. Hover to see how it applies.
What to read next
Disable a timer at every level of your ASP.NET control hierarchy
Learn how to recursively find and disable all Timer controls in an ASP.NET page, even without knowing their IDs, to prevent unwanted UI …
My first Extension method
Explains how to create and use extension methods in VB.NET, with an example for enhancing the XboxInfo class to display and track Xbox Live …
Wpf Drag & Drop behaviour
Explains how to implement flexible drag and drop in WPF using MVVM, with customisable drop behaviour and bindable options for ItemsControls, …
VSTS Sync Migration Tools
Open-source tools for migrating work items, test plans, teams, and project structures between TFS and VSTS, supporting consolidation and …
Detecting agile theatre with real delivery signals
Why Most Companies Operating Models Fail in Dynamic Markets
A concise comparison of Predictive and Adaptive Operating Models, explaining why traditional structures fail in dynamic markets and how …
Don’t Manage Dependencies, Remove Them
Explains why dependencies are a sign of poor system design and outlines steps to eliminate them by aligning teams, clarifying ownership, and …
The Estimation Trap: How Tracking Accuracy Undermines Trust, Flow, and Value in Software Delivery
Tracking estimation accuracy in software delivery leads to mistrust, fear, and distorted behaviours. Focus on customer value, flow, and …
Flow of Value vs Flow of Work – Misnomer or Useful Shorthand?
Compares “flow of value” and “flow of work” in Kanban, explaining why only validated outcomes count as value and stressing the need for …
Why Outsourcing DevOps Fails, and How Real Engineering Excellence Starts With Your Team
Avoid DevOps vendor lock-in, discover how true engineering excellence starts with partnership, not outsourcing. Ready to transform your …