Perform set operations such as union and intersection on enumerable lists in C#

This example creates two sets of Person objects. The Person class implements IEquatable.

The code then uses the following code to display the lists and their intersection, union, difference, and exclusive or (xor).

// Display the values.
lstA.DataSource = list_a;
lstB.DataSource = list_b;
lstAUnionB.DataSource = list_a.Union(list_b).ToList();
lstAIntersectB.DataSource = list_a.Intersect(list_b).ToList();
lstAMinusB.DataSource = list_a.Except(list_b).ToList();
lstAXorB.DataSource = list_a.Xor(list_b);

The code first sets the lstA and lstB DataSource properties so those ListBoxes display the original lists. It then uses the LINQ Union, Intersect, and Except methods to get the union, intersection, and exclusion of the lists. The Except method returns the items in list A except those that are in list B.

Notice that the union and intersection methods do not include duplicates. For example, Art Archer appears in both lists but it appears only once in the union. In fact, Bev Baker appears twice in list B but it still only appears once in the union.

The final line of code uses the Xor method. For some reason, LINQ doesn’t include this operation so I implemented it in the following extension method.

public static class ListExtensions
public static IEnumerable Xor(this IEnumerable list_a, IEnumerable list_b)
IEnumerable intersection = list_a.Intersect(list_b).ToList();
IEnumerable only_a = list_a.Except(intersection).ToList();
IEnumerable only_b = list_b.Except(intersection).ToList();
return only_a.Union(only_b);

This code finds the intersection of the two lists. It then uses the Except method to make versions of the lists that don’t include their intersection. Finally it combines the two to get the Xor of the original lists.

In other words, the code uses this identity:

    A ? B = (A - B) ? (B - A)


This entry was posted in mathematics. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *