Enumerable.Except returns distinct items

Yan Cui

I help clients go faster for less using serverless technologies.

This one took me by surprise a little but there’s something about the Enumerable.Except method which you ought to know about. At first you might have assumed (as I did) that given an array as the base set and calling the Except extension method with a second array will return all the items in the first array which do not appear in the second array.

Well, not exactly, for instance:

var list1 = new[] { 1, 1, 1, 1 };
var list2 = Enumerable.Empty<int>();

var result = list1.Except(list2); // returns { 1 } instead of the expected { 1, 1, 1, 1 }

var list3 = new[] { 1, 1, 2, 2, 3 };
var list4 = new[] { 1 };

var result2 = list3.Except(list4); // returns { 2, 3 } instead of the expected { 2, 2, 3 }

Does it surprise you that only distance instances of the original array is returned by the Except method? Whilst I haven’t found any official Microsoft documentation/article on why Except behaves in this way, the best explanation I have heard so far is that Except performs a set operation, according to MSDN:

Produces the set difference of two sequences by using the default equality comparer to compare values.

And a Set in the mathematical sense is defined as:

A set is a collection of distinct objects, considered as an object in its own right


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


1 thought on “Enumerable.Except returns distinct items”

  1. Pingback: LINQ – Some pitfalls to look out for | theburningmonk.com

Leave a Comment

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