Linq101-Projection
1 using System; 2 using System.Linq; 3 4 namespace Linq101 5 { 6 class Projection 7 { 8 /// <summary> 9 /// This sample uses select to produce a sequence of ints one higher than those in an existing array of ints. 10 /// </summary> 11 public void Linq6() 12 { 13 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 14 15 var query = from n in numbers 16 select n + 1; 17 18 Console.WriteLine("Numbers + 1 :"); 19 foreach (var i in query) 20 { 21 Console.WriteLine(i); 22 } 23 } 24 25 /// <summary> 26 /// This sample uses select to return a sequence of just the names of a list of products. 27 /// </summary> 28 public void Linq7() 29 { 30 var products = Data.GetProductList(); 31 32 var query = from p in products 33 select p.ProductName; 34 35 Console.WriteLine("Product Names:"); 36 foreach (var productName in query) 37 { 38 Console.WriteLine(productName); 39 } 40 } 41 42 /// <summary> 43 /// This sample uses select to produce a sequence of strings representing the text version of a sequence of ints. 44 /// </summary> 45 public void Linq8() 46 { 47 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 48 string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 49 50 var query = from n in numbers 51 select strings[n]; 52 53 Console.WriteLine("Number strings:"); 54 foreach (var s in query) 55 { 56 Console.WriteLine(s); 57 } 58 } 59 60 /// <summary> 61 /// This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array. 62 /// </summary> 63 public void Linq9() 64 { 65 string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; 66 67 var query = from w in words 68 select new { U = w.ToUpper(), L = w.ToLower() }; 69 70 Console.WriteLine("Results:"); 71 foreach (var item in query) 72 { 73 Console.WriteLine("Uppercase:{0},Lowercase:{1}", item.U, item.L); 74 } 75 } 76 77 /// <summary> 78 /// This sample uses select to produce a sequence containing text representations of digits and whether their length? is even or odd. 79 /// </summary> 80 public void Linq10() 81 { 82 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 83 string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 84 85 var query = from n in numbers 86 select new { Digit = strings[n], EorO = n % 2 == 0 ? "Even" : "Odd" }; 87 88 Console.WriteLine("Results:"); 89 foreach (var item in query) 90 { 91 Console.WriteLine("The digit {0} is {1}", item.Digit, item.EorO); 92 } 93 } 94 95 /// <summary> 96 /// This sample uses select to produce a sequence containing some properties of Products, including UnitPrice which is renamed to Price in the resulting type. 97 /// </summary> 98 public void Linq11() 99 { 100 var products = Data.GetProductList(); 101 102 var query = from p in products 103 select new { p.ProductName, p.Category, Price = p.UnitPrice }; 104 105 Console.WriteLine("Product Info:"); 106 foreach (var product in query) 107 { 108 Console.WriteLine("{0} is in the category {1} and cost {2} per unit", product.ProductName, product.Category, product.Price); 109 } 110 } 111 112 /// <summary> 113 /// This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array. 114 /// </summary> 115 public void Linq12() 116 { 117 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 118 119 var query = numbers.Select((n, index) => new { Num = n, InPlace = n == index }); 120 121 Console.WriteLine("Number:In-place?"); 122 foreach (var number in query) 123 { 124 Console.WriteLine("{0}:{1}", number.Num, number.InPlace); 125 } 126 } 127 128 /// <summary> 129 /// This sample combines select and where to make a simple query that returns the text form of each digit less than 5. 130 /// </summary> 131 public void Linq13() 132 { 133 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 134 string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 135 136 var query = from n in numbers 137 where n < 5 138 select digits[n]; 139 140 Console.WriteLine("Numbers < 5:"); 141 foreach (var digit in query) 142 { 143 Console.WriteLine(digit); 144 } 145 } 146 147 /// <summary> 148 /// This sample uses a compound from clause to make a query that returns all pairs of numbers from both arrays such that the number from numbersA is less than the number from numbersB. 149 /// </summary> 150 public void Linq14() 151 { 152 int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 153 int[] numbersB = { 1, 3, 5, 7, 8 }; 154 155 var query = from a in numbersA 156 from b in numbersB 157 where a < b 158 select new { a, b }; 159 160 Console.WriteLine("Pairs where a < b"); 161 foreach (var pair in query) 162 { 163 Console.WriteLine("{0} is less than {1}", pair.a, pair.b); 164 } 165 } 166 167 /// <summary> 168 /// This sample uses a compound from clause to select all orders where the order total is less than 500.00. 169 /// </summary> 170 public void Linq15() 171 { 172 var customers = Data.GetCustomerList(); 173 174 var query = from c in customers 175 from o in c.Orders 176 where o.Total < 500 177 select new { c.CustomerID, o.OrderID, o.Total }; 178 179 ObjectDumper.Write(query); 180 } 181 182 /// <summary> 183 /// This sample uses a compound from clause to select all orders where the order was made in 1998 or later. 184 /// </summary> 185 public void Linq16() 186 { 187 var customers = Data.GetCustomerList(); 188 189 var query = from c in customers 190 from o in c.Orders 191 where o.OrderDate >= new DateTime(1998, 1, 1) 192 select new { c.CustomerID, o.OrderID, o.OrderDate }; 193 194 ObjectDumper.Write(query); 195 } 196 197 /// <summary> 198 /// This sample uses a compound from clause to select all orders where the order total is greater than 2000.00 and uses from assignment to avoid requesting the total twice. 199 /// </summary> 200 public void Linq17() 201 { 202 var customers = Data.GetCustomerList(); 203 204 var query = from c in customers 205 from o in c.Orders 206 where o.Total > 2000 207 select new { c.CustomerID, o.OrderID, o.Total }; 208 209 ObjectDumper.Write(query); 210 } 211 212 /// <summary> 213 /// This sample uses multiple from clauses so that filtering on customers can be done before selecting their orders. This makes the query more efficient by not selecting and then discarding orders for customers outside of Washington. 214 /// </summary> 215 public void Linq18() 216 { 217 var customers = Data.GetCustomerList(); 218 219 //效率低 220 //var query = from c in customers 221 // from o in c.Orders 222 // where c.Region == "WA" && o.OrderDate >= new DateTime(1997, 1, 1) 223 // select new { c.CustomerID, o.OrderID }; 224 225 var query = from c in customers 226 where c.Region == "WA" 227 from o in c.Orders 228 where o.OrderDate >= new DateTime(1997, 1, 1) 229 select new { c.CustomerID, o.OrderID }; 230 231 ObjectDumper.Write(query); 232 } 233 234 /// <summary> 235 /// This sample uses an indexed SelectMany clause to select all orders, while referring to customers by the order in which they are returned from the query. 236 /// </summary> 237 public void Linq19() 238 { 239 var customers = Data.GetCustomerList(); 240 241 //var query = customers.SelectMany(c => c.Orders); 242 //var query = customers.SelectMany(c => c.Orders).Where(o => o.OrderDate >= new DateTime(1998, 1, 1)); 243 var query = 244 customers.SelectMany( 245 (customer, index) => 246 (customer.Orders.Select(o => "Customer #" + (index + 1) + " has an order with OrderID " + o.OrderID))); 247 248 ObjectDumper.Write(query); 249 } 250 } 251 }