thzinc

Think LINQ

“Think LINQ” is a short series of posts I wrote in 2012 because I was enamored with the iterator pattern and its relatively-recent generic implementation within .NET Framework 2.0. I really enjoy the way that the iterator pattern enables the composition of programs. LINQ never really took off the way that Microsoft had pushed it initially, but the supporting libraries around it are some of my favorite examples of good API design.

Think LINQ

Think LINQ – The Wonder of IEnumerable<>

Language INtegrated Query, or LINQ, is a .NET feature that makes possible a powerful and extensible query on objects and collections thereof. LINQ is really a combination of a few key components: extension methods and generic collections. Understanding these two key components makes it much easier to “Think LINQ” when…
Think LINQ

Think LINQ – .Cast()

Personally, I find that .Cast() is an often overlooked part of LINQ. Of course, .Cast() is handy when casting each element of an IEnumerable<> from one type to another. However, one detail in its method signature brings to light a much more interesting use: .Cast() extends IEnumerable, the non-generic interface,…
Think LINQ

Think LINQ – .ToDictionary()

There are a number of reasons to use Dictionary<> objects. Aside from the obvious name-value pair uses, Dictionary<> can also be used to essentially “index” an IEnumerable<> of objects. In testing with my colleague, Ryan Davis, we found that for IEnumerable<> collections that we intended to search through on a…
Think LINQ

Think LINQ – .ToLookup()

There is an class in the .NET generic collection framework that is often overlooked: Lookup<>. In effect, a Lookup<> functions like a Dictionary<> whose value is an IEnumerable<>. Though Lookup<T,U> is an implementation of IEnumerable<IGrouping<T, U>>, it functions with a similar efficiency to Dictionary<T,IEnumerable<U>>. Part of the LINQ extension methods…
Think LINQ

Think LINQ – .SelectMany()

I recently came across a beautiful example of .SelectMany() used to find all types that implement a particular interface in all currently loaded assemblies. With minor alterations, here is how I used it:
Think LINQ

Think LINQ – .Any() and .All()

“I just want to know if there’s anything in this List.” “Do any of the strings in my array start with ‘q’?” “How can I be sure all of the Rectangles in my IEnumerable<> have a width of 10?” These are the types of questions .Any() and .All() can answer.…
Think LINQ

Think LINQ – .Skip() and .Take()

Needing to page through a collection is nothing new, and LINQ handles this nicely with two different methods: .Skip() and .Take(). The .Skip() method will skip over a specified number of items in an IEnumerable<>. The .Take() method will iterate over a specified number of items of an IEnumerable<> and…
Think LINQ

Think LINQ – .SequenceEqual()

Up until March 26, I had come up with a number of crazy concoctions to test whether one IEnumerable<> was equal to another. Some of them chained together .Intersect() with .Count(), comparing the count of the elements in the intersected set with the count of the elements in each of…