In a post last year I explored how we can use LINQ to batch or page items from a collection in such away as to return a sequence of sub-sequence, each having equal length (except for possibly the last one). In that post I made a significant mistake that I would like to correct here.
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.
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:
As an example of how multiple LINQ methods can be combined for interesting results we will use
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.
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:
Join(). These all take two input collections, and combine their elements into a single resulting collection in different ways.
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
Over the last few weeks I have been writing about the basics of LINQ, and how we can use it to organize collections of items.
Today I would like to take a look at a special class of LINQ methods: those returning single elements or values, instead of an entire collection.
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.
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.
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.