LINQ and the use of Repeat and Range operator

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


No Comments