LINQ and the use of Repeat and Range operator

Posted by vik20000in on ASP.net Weblogs See other posts from ASP.net Weblogs or by vik20000in
Published on Tue, 20 Apr 2010 00:22:00 GMT Indexed on 2010/04/20 0:33 UTC
Read the original article Hit count: 494

Filed under:
|
|
|

LINQ is also very useful when it comes to generation of range or repetition of data.  We can generate a range of data with the help of the range method.

    var numbers =

        from n in Enumerable.Range(100, 50)

        select new {Number = n, OddEven = n % 2 == 1 ? "odd" : "even"};


The above query will generate 50 records where the record will start from 100 till 149. The query also determines if the number is odd or even.

But if we want to generate the same number for multiple times then we can use the Repeat method.

    var numbers = Enumerable.Repeat(7, 10);


The above query will produce a list with 10 items having the value 7.

Vikram


© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about ASP.NET