Showing posts from March, 2010

Linq Remove Duplicates and Merge Two Collections using Union and Distinct

Linq is a boon from Microsoft to all .net developers; it makes things really easy and simple and also gives the ability to perform several tasks that you do using sql (many more).I had to remove duplicates based on some columns from two different collections, it was done using for loops in the past, as linq is available I thought of giving it a shot.

Union and distinct is used to obtain the desired results; also notice a custom IEqualityComparer is passed to distinct instead of the default equality comparer to compare values. In custom equality comparer you can ignore the columns or properties you don’t want to consider when deciding the uniqueness. If you don’t have a custom IEqualityComparer then linq distinct wont work as expected and might return duplicates.

Let’s dive into some code here.
''define lists
Dim list1 As New List(Of Item)
Dim list2 As New List(Of Item)

''add two items to list 1
Dim item1 As New Item
item1.ItemID = Guid.Empty

Complex and a special three state (tri state) Treeview Control

As we all know treeview is used to show hierarchy of items easily. Microsoft tree is readily available for you if your needs are simple. But if you want to have the tree do more than two states it might not be possible with the treeview control as it is. You might have seen several treeview .net controls out there that have three states but the treeview in this post here might be little different than those. A parent can have its own state and can depict its child check state when stateless, also all the children under a parent are not affected when check state on the parent changes.

Checked – Green Check
Unchecked – No Check
AtLeastOneChild Checked (only for Parent) – Light Gray Check

State Determination:

Node when checked a green check is shown regardless of whether any child nodes have been checked
Node when unchecked but has checked child, gray check is shown for the node.
Node when unchecked and has no children or all children unchecked, an empty check is shown

State Images:

What i…