LINQ Short Takes – Number 1 – Enumerable.Range()


This blog post is the first of a series of shorter posts that will cover a number of brief LINQ topics.

This post will cover the Enumerable.Rangemethod. This is a ‘for free’ (part of the .Net Framework) method that I have found very useful. There have been a number of circumstances where I have used this method. This method offers a simple data source vital to addressing a number of classes of problems. With the use of this simple data source, some problems become trivial, rather than extremely complex with ‘vanilla’ LINQ.

There are a couple of subsequent posts, I will write, which build on the use of the Enumerable.Range method.

The Enumerable Range Method

This is quite a simple method. The MSDN documentation (see: Enumerable.Range) describes the method as follows.

“Generates a sequence of integral numbers within a specified range.”

This statement is almost dismissive of the many ways that this method provides a very flexible data source.

Some Examples Using Enumerable.Range

The following are a couple of simple examples of the use of the Enumerable.Range.

private void Simple_Range_Demos()
    // A Simple Range Example var Test1 = from seq in Enumerable.Range(0, 10)
                select seq;

    // Build a year calendar using Range DateTime Jan1 = new DateTime(2012, 1, 1);
    int Days = 0;
    if (DateTime.IsLeapYear(2012)) Days = 366;
    else Days = 365;
    var YearCalendar = from seq in Enumerable.Range(0, Days)
                       select Jan1.AddDays(seq);


These presented examples may seem trite and overly simple. In forthcoming post I will build upon the use of the Enumerable.Range to achieve some interesting, and useful LINQ techniques.


, , , , , , ,

  1. LINQ Pivot, or Crosstab, Extension Method « Craig's Eclectic Blog
  2. LINQ Short Takes – Number 3 –LINQ over Multiple Dimension Arrays and Lists « Craig's Eclectic Blog
  3. LINQ Short Takes – Number 2 – Using Method Syntax to Create a Cartesian Product « Craig's Eclectic Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: