LINQ: Writing an Interweave extension

As we have seen in our last few posts on LINQ, the framework comes with some great extension methods to handle the merging of multiple collections into a single one.

However, there are things that LINQ can not easily do on its own. One of them is interweaving collections, i.e. merging them by taking elements from each input collection alternatively.

For example, given the inputs 1, 2, 3 and a, b, c we would get the output 1, a, 2, b, 3, c.

Today, we will look into how to best implement this ourselves.

Continue reading →

LINQ: Flatten nested collections with SelectMany

Last time we looked at some of the possible ways of using LINQ to merge two collections into one.

Today we are going to look at another method that can be used to combine collections: SelectMany.

As an example of how multiple LINQ methods can be combined for interesting results we will use SelectMany and Zip to quickly implement a merging operation that LINQ does not have a method for: Interweaving elements from multiple collections.

Spoiler: Our interweaving implementation with SelectMany will be nice and short, but it will be sub-optimal with regards to performance. We will tackle that problem in a follow up post.

Continue reading →

LINQ: merging collections with Concat, Zip, and Join

Continuing my series of posts on LINQ, today I want to write about a few of the LINQ extension methods that take multiple input collections and return a single one.

Specifically, I want to take a look at the following methods: Concat(), Zip() and Join(). These all take two input collections, and combine their elements into a single resulting collection in different ways.

Continue reading →

Linq and set theory: Union() and Intersect()

So far, in my series of posts on LINQ we have covered only methods acting on single collections. Today we are going to look at some of the LINQ methods that take a second collection as parameter.

Specifically, we will at the classical set theory operators of union, intersection, and relative complement, which in LINQ are implemented with the Union() and Intersect() methods.

Continue reading →

LINQ: from IEnumerable to concrete collections

I my recent posts introducing LINQ from a game developers point of view, I mentioned several times how the many LINQ methods returning sequences of the IEnumerable<T> type do not actually return an actual collection.

Instead they return a query that can be executed any number of time on the given input collection.

Of course, there comes a point at which we need to store the results of such queries as regular collections. Today we will talk about how LINQ supports this almost trivially.

Continue reading →

Sorting and Grouping – organizing data with LINQ

Last week I introduced LINQ from the perspective of a C# game developer completely unfamiliar with the framework. Today I would like to continue exploration of LINQ by focussing on a particular set of its functionality: methods to arrange and organize data.

In particular we will look into how we can sort and group our collections of items.

Continue reading →

LINQ – a game development focused introduction

I was recently asked for some pointers on how to get started with LINQ – and to maybe write a post about that. Using LINQ virtually every day I have to admit that it had not occurred to me that a C# programmer may not be familiar with it.

LINQ is a big topic, but this post is the first in a series to introduce the framework and its many uses – all from a game developer’s point of view.

Continue reading →