Linq101-Partitioning

  1 using System;
  2 using System.Linq;
  3 
  4 namespace Linq101
  5 {
  6     class Partitioning
  7     {
  8         /// <summary>
  9         /// This sample uses Take to get only the first 3 elements of the array.
 10         /// </summary>
 11         public void Linq20()
 12         {
 13             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 14 
 15             var query = numbers.Take(3);
 16 
 17             Console.WriteLine("First 3 numbers:");
 18             foreach (var n in query)
 19             {
 20                 Console.WriteLine(n);
 21             }
 22         }
 23 
 24         /// <summary>
 25         /// This sample uses Take to get the first 3 orders from customers in Washington.
 26         /// </summary>
 27         public void Linq21()
 28         {
 29             var customers = Data.GetCustomerList();
 30 
 31             var query = (from c in customers
 32                          from o in c.Orders
 33                          where c.Region == "WA"
 34                          select new { c.CustomerID, o.OrderID, o.OrderDate })
 35                 .Take(3);
 36 
 37             //var query = (from c in customers
 38             //             where c.Region == "WA"
 39             //             from o in c.Orders
 40             //             select new { c.CustomerID, o.OrderID, o.OrderDate })
 41             //    .Take(3);
 42 
 43             Console.WriteLine("First 3 orders in WA:");
 44             foreach (var order in query)
 45             {
 46                 ObjectDumper.Write(order);
 47             }
 48         }
 49 
 50         /// <summary>
 51         /// This sample uses Skip to get all but the first 4 elements of the array.
 52         /// </summary>
 53         public void Linq22()
 54         {
 55             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 56 
 57             var query = numbers.Skip(4);
 58 
 59             Console.WriteLine("All but first 4 numbers:");
 60             foreach (var n in query)
 61             {
 62                 Console.WriteLine(n);
 63             }
 64         }
 65 
 66         /// <summary>
 67         /// This sample uses Take to get all but the first 2 orders from customers in Washington.
 68         /// </summary>
 69         public void Linq23()
 70         {
 71             var customers = Data.GetCustomerList();
 72 
 73             var query = (from c in customers
 74                          where c.Region == "WA"
 75                          from o in c.Orders
 76                          select new { c.CustomerID, o.OrderID, o.OrderDate })
 77                 .Skip(2);
 78 
 79             foreach (var order in query)
 80             {
 81                 ObjectDumper.Write(order);
 82             }
 83         }
 84 
 85         /// <summary>
 86         /// This sample uses TakeWhile to return elements starting from the beginning of the array until a number is hit that is not less than 6.
 87         /// </summary>
 88         public void Linq24()
 89         {
 90             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 91 
 92             var query = numbers.TakeWhile(n => n < 6);
 93 
 94             foreach (var n in query)
 95             {
 96                 Console.WriteLine(n);
 97             }
 98         }
 99 
100         /// <summary>
101         /// This sample uses TakeWhile to return elements starting from the beginning of the array until a number is hit that is less than its position in the array.
102         /// </summary>
103         public void Linq25()
104         {
105             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
106 
107             var query = numbers.TakeWhile((number, index) => number > index);
108 
109             foreach (var n in query)
110             {
111                 Console.WriteLine(n);
112             }
113         }
114 
115         /// <summary>
116         /// This sample uses SkipWhile to get the elements of the array starting from the first element divisible by 3.
117         /// </summary>
118         public void Linq26()
119         {
120             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
121 
122             var query = numbers.SkipWhile(n => n % 3 != 0);
123 
124             foreach (var n in query)
125             {
126                 Console.WriteLine(n);
127             }
128         }
129 
130         /// <summary>
131         /// This sample uses SkipWhile to get the elements of the array starting from the first element less than its position.
132         /// </summary>
133         public void Linq27()
134         {
135             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
136 
137             var query = numbers.SkipWhile((number, index) => number > index);
138 
139             foreach (var n in query)
140             {
141                 Console.WriteLine(n);
142             }
143         }
144     }
145 }

 

posted @ 2014-12-22 17:01  David Huang  阅读(185)  评论(0编辑  收藏  举报