Linq101-Generation

 1 using System;
 2 using System.Linq;
 3 
 4 namespace Linq101
 5 {
 6     class Generation
 7     {
 8         /// <summary>
 9         /// This sample uses Range to generate a sequence of numbers from 100 to 149 that is used to find which numbers in that range are odd and even.
10         /// </summary>
11         public void Linq65()
12         {
13             var numbers = from n in Enumerable.Range(100, 50)
14                           select new { Number = n, OddEven = n % 2 == 1 ? "odd" : "even" };
15 
16             foreach (var n in numbers)
17             {
18                 Console.WriteLine("The number {0} is {1}.", n.Number, n.OddEven);
19             }
20         }
21 
22         /// <summary>
23         /// This sample uses Repeat to generate a sequence that contains the number 7 ten times.
24         /// </summary>
25         public void Linq66()
26         {
27             var numbers = Enumerable.Repeat(7, 10);
28 
29             foreach (var n in numbers)
30             {
31                 Console.WriteLine(n);
32             }
33         }
34     }
35 }

 

posted @ 2015-03-09 16:33  David Huang  阅读(217)  评论(0编辑  收藏  举报