LINQ and the use of Repeat and Range operator
Posted on 4/19/2010 8:26:40 PM
in #Asp.Net 3.X
LINQ is also very useful when it comes to generation of range or repetition of data. /SPAN>We can generate a range of data with the help of the range method./P>
/SPAN>var numbers =/P>
/SPAN>from n in Enumerable.Range(100, 50)/P>
/SPAN>select new {Number = n, OddEven = n % 2 == 1 ? "odd" : "even"};/P>
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./P>
But if we want to generate the same number for multiple times then we can use the Repeat method. /P>
/SPAN>var numbers = Enumerable.Repeat(7, 10);/P>
The above query will produce a list with 10 items having the value 7./P>
Vikram/P>
|