Posts Tagged year calendar

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


Introduction

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;
    Test1.ToOutput();

    // 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);
    return;
}

Conclusions

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.

Advertisements

, , , , , , ,

3 Comments

%d bloggers like this: