33public static class Basics
44{
55
6- static readonly int [ ] scores = [ 0 ] ; // Max is called on this, so one value is needed
6+ static readonly int [ ] scores = [
7+ 0 , 30 , 50 , 70 , 80 , 85 , 94 , 87 , 96 , 88 , 59 , 90 , 91 , 85 , 60 , 49 , 100
8+ ] ;
79
810 // <SourceData>
911 static readonly City [ ] cities = [
@@ -50,6 +52,10 @@ where score > 80
5052 orderby score descending
5153 select score ;
5254 // </basics1>
55+ foreach ( var score in highScoresQuery )
56+ {
57+ Console . WriteLine ( score ) ;
58+ }
5359 }
5460
5561 public static void Basics2 ( )
@@ -61,6 +67,10 @@ where score > 80
6167 orderby score descending
6268 select $ "The score is { score } ";
6369 // </basics2>
70+ foreach ( var score in highScoresQuery2 )
71+ {
72+ Console . WriteLine ( score ) ;
73+ }
6474 }
6575
6676 public static void Basics3 ( )
@@ -72,6 +82,7 @@ where score > 80
7282 select score
7383 ) . Count ( ) ;
7484 // </basics3>
85+ Console . WriteLine ( $ "highest score: { highScoreCount } ") ;
7586 }
7687
7788 public static void Basics4 ( )
@@ -84,6 +95,7 @@ where score > 80
8495
8596 var scoreCount = highScoresQuery3 . Count ( ) ;
8697 // </basics4>
98+ Console . WriteLine ( $ "highest score: { scoreCount } ") ;
8799 }
88100
89101 public static void Basics5 ( )
@@ -122,7 +134,7 @@ public static void Basics6()
122134 //Query syntax
123135 IEnumerable < City > queryMajorCities =
124136 from city in cities
125- where city . Population > 100000
137+ where city . Population > 30_000_000
126138 select city ;
127139
128140 // Execute the query to produce the results
@@ -132,12 +144,19 @@ where city.Population > 100000
132144 }
133145
134146 // Output:
135- // City { Population = 120000 }
136- // City { Population = 112000 }
137- // City { Population = 150340 }
138-
147+ // City { Name = Tokyo, Population = 37833000 }
148+ // City { Name = Delhi, Population = 30290000 }
149+
139150 // Method-based syntax
140- IEnumerable < City > queryMajorCities2 = cities . Where ( c => c . Population > 100000 ) ;
151+ IEnumerable < City > queryMajorCities2 = cities . Where ( c => c . Population > 30_000_000 ) ;
152+ // Execute the query to produce the results
153+ foreach ( City city in queryMajorCities2 )
154+ {
155+ Console . WriteLine ( city ) ;
156+ }
157+ // Output:
158+ // City { Name = Tokyo, Population = 37833000 }
159+ // City { Name = Delhi, Population = 30290000 }
141160 // </basics6>
142161 }
143162
@@ -158,6 +177,8 @@ from score in scores
158177 // the following returns the same result
159178 highScore = scores . Max ( ) ;
160179 // </basics7>
180+ Console . WriteLine ( $ "highest score: { highestScore } ") ;
181+ Console . WriteLine ( $ "high Score: { highScore } ") ;
161182 }
162183
163184 public static void Basics7a ( )
@@ -178,6 +199,14 @@ where city.Population > 10000
178199 select city ;
179200 var largeCitiesList2 = largeCitiesQuery . ToList ( ) ;
180201 // </basics7a>
202+ foreach ( var item in largeCitiesList )
203+ {
204+ Console . WriteLine ( item . Name + ":" + item . Population ) ;
205+ }
206+ foreach ( var item in largeCitiesList2 )
207+ {
208+ Console . WriteLine ( item . Name + ":" + item . Population ) ;
209+ }
181210 }
182211
183212 public static void Basics8 ( )
@@ -188,16 +217,24 @@ from city in cities
188217 where city . Population > 100000
189218 select city ;
190219 // </basics8>
220+ foreach ( var city in queryCities )
221+ {
222+ Console . WriteLine ( city . Name + ":" + city . Population ) ;
223+ }
191224 }
192225
193226 public static void Basics9 ( )
194227 {
195228 // <basics9>
196229 IEnumerable < Country > countryAreaQuery =
197230 from country in countries
198- where country . Area > 500000 //sq km
231+ where country . Area > 20 //sq km
199232 select country ;
200233 // </basics9>
234+ foreach ( var country in countryAreaQuery )
235+ {
236+ Console . WriteLine ( country . Name + ":" + country . Area ) ;
237+ }
201238 }
202239
203240 public static void Basics10 ( )
@@ -209,6 +246,10 @@ from city in country.Cities
209246 where city . Population > 10000
210247 select city ;
211248 // </basics10>
249+ foreach ( var city in cityQuery )
250+ {
251+ Console . WriteLine ( city . Name + ":" + city . Population ) ;
252+ }
212253 }
213254
214255 public static void Basics11 ( )
@@ -218,6 +259,14 @@ public static void Basics11()
218259 from country in countries
219260 group country by country . Name [ 0 ] ;
220261 // </basics11>
262+ foreach ( var group in queryCountryGroups )
263+ {
264+ Console . WriteLine ( group . Key ) ;
265+ foreach ( var country in group )
266+ {
267+ Console . WriteLine ( country . Name ) ;
268+ }
269+ }
221270 }
222271
223272 public static void Basics12 ( )
@@ -228,6 +277,10 @@ from country in countries
228277 orderby country . Area
229278 select country ;
230279 // </basics12>
280+ foreach ( var country in sortedQuery )
281+ {
282+ Console . WriteLine ( country . Name + ":" + country . Area ) ;
283+ }
231284 }
232285
233286 public static void Basics13 ( )
@@ -241,6 +294,10 @@ from country in countries
241294 Pop = country . Population
242295 } ;
243296 // </basics13>
297+ foreach ( var item in queryNameAndPop )
298+ {
299+ Console . WriteLine ( item . Name + ":" + item . Pop ) ;
300+ }
244301 }
245302
246303 public static void Basics14 ( )
@@ -249,7 +306,7 @@ public static void Basics14()
249306 // percentileQuery is an IEnumerable<IGrouping<int, Country>>
250307 var percentileQuery =
251308 from country in countries
252- let percentile = ( int ) country . Population / 10_000_000
309+ let percentile = ( int ) country . Population / 1_000
253310 group country by percentile into countryGroup
254311 where countryGroup . Key >= 20
255312 orderby countryGroup . Key
@@ -272,9 +329,13 @@ public static void Basics15()
272329 // <basics15>
273330 IEnumerable < City > queryCityPop =
274331 from city in cities
275- where city . Population is < 200000 and > 100000
332+ where city . Population is < 15_000_000 and > 10_000_000
276333 select city ;
277334 // </basics15>
335+ foreach ( var city in queryCityPop )
336+ {
337+ Console . WriteLine ( city . Name + ":" + city . Population ) ;
338+ }
278339 }
279340
280341 public static void Basics16 ( )
@@ -285,12 +346,24 @@ from country in countries
285346 orderby country . Area , country . Population descending
286347 select country ;
287348 // </basics16>
349+ foreach ( var country in querySortedCountries )
350+ {
351+ Console . WriteLine ( country . Name + ":" + country . Area + ":" + country . Population ) ;
352+ }
288353 }
289354
290355 public static void Basics17 ( )
291356 {
292- string [ ] categories = [ ] ;
293- Product [ ] products = [ ] ;
357+ string [ ] categories = [ "brass" , "winds" , "percussion" ] ;
358+ Product [ ] products = [
359+ new Product ( "Trumpet" , "brass" ) ,
360+ new Product ( "Trombone" , "brass" ) ,
361+ new Product ( "French Horn" , "brass" ) ,
362+ new Product ( "Clarinet" , "winds" ) ,
363+ new Product ( "Flute" , "winds" ) ,
364+ new Product ( "Cymbal" , "percussion" ) ,
365+ new Product ( "Drum" , "percussion" )
366+ ] ;
294367
295368 // <basics17>
296369 var categoryQuery =
@@ -302,6 +375,10 @@ join prod in products on cat equals prod.Category
302375 Name = prod . Name
303376 } ;
304377 // </basics17>
378+ foreach ( var item in categoryQuery )
379+ {
380+ Console . WriteLine ( item . Category + ":" + item . Name ) ;
381+ }
305382 }
306383
307384 public static void Basics18 ( )
@@ -339,5 +416,9 @@ select student2.ExamScores.Average()
339416 ) . Max ( )
340417 } ;
341418 // </basics19>
419+ foreach ( var item in queryGroupMax )
420+ {
421+ Console . WriteLine ( item . Level + ":" + item . HighestScore ) ;
422+ }
342423 }
343424}
0 commit comments