linq 实际练习题(原创)
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace LmbdaText
7 {
8 class Program
9 {
10 staticint[] numbers =newint[]{5,3,4,2,1,9,6,7,8,0 };
11 staticstring[] strings =newstring[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
12 staticobject[] obj={"2a",12,21.22,};
13
14
15 static Product[] product =
16 {
17 new Product{ProductId=5,ProductName="Berry"},
18 };
19 static Product[] product2 =
20 {
21 new Product{ProductId=1,ProductName="Apple"},
22 new Product{ProductId=4,ProductName="Orange"},
23 new Product{ProductId=5,ProductName="Berry"},
24 };
25 static Product[] product3 =
26 {
27
28 new Product{ProductId=4,ProductName="Orange"},
29 new Product{ProductId=5,ProductName="Berry"},
30 };
31 static Product[] product4 =
32 {
33 new Product{ProductId=1,ProductName="Apple"},
34 new Product{ProductId=2,ProductName="Banana"},
35 new Product{ProductId=3,ProductName="Grape"}
36
37 };
38 static PersonDetail[] personDetail =
39 {
40
41 new PersonDetail{Id=1, Hobby="hobby1", Ie=product.ToList<Product>()},
42 new PersonDetail{Id=3, Hobby="hobby3", Ie=product2.ToList<Product>()},
43 new PersonDetail{Id=5, Hobby="hobby5", Ie=product3.ToList<Product>()},
44 new PersonDetail{Id=7, Hobby="hobby7", Ie=product4.ToList<Product>()},
45 new PersonDetail{Id=2, Hobby="hobby2", Ie=product.ToList<Product>()},
46 };
47
48 static Person[] person =
49 {
50 new Person {Id=1,Name="Matt", Level=3,personDetail=new PersonDetail
51 { Id=1, Hobby="Hobby1",Ie=product2.ToList<Product>()},
52 },
53 new Person {Id=2,Name="Luca", Level=3,personDetail=new PersonDetail
54 { Id=2, Hobby="Hobby1",Ie=product.ToList<Product>()},},
55 new Person {Id=3,Name="Jomo", Level=5,personDetail=new PersonDetail
56 { Id=3, Hobby="Hobby1",Ie=product3.ToList<Product>()},},
57 new Person {Id=4,Name="Dinesh", Level=3,personDetail=new PersonDetail
58 { Id=4, Hobby="Hobby1",Ie=product4.ToList<Product>()},},
59 new Person {Id=5,Name="Charlie", Level=3,personDetail=new PersonDetail
60 { Id=5, Hobby="Hobby1",Ie=product2.ToList<Product>()},},
61 new Person {Id=6,Name="Mads", Level=3,personDetail=new PersonDetail
62 { Id=6, Hobby="Hobby1",Ie=product.ToList<Product>()},},
63 new Person {Id=7,Name="Anders", Level=9,personDetail=new PersonDetail
64 { Id=7, Hobby="Hobby1",Ie=product.ToList<Product>()},}
65 };
66
67 staticvoid Main(string[] args)
68 {
69 Sample24();
70
71
72 }
73
74 staticvoid Sample()
75 {
76 IEnumerable<int> nums = numbers.Where(x => x <5);
77 foreach (int i in nums)
78 {
79 Console.Write(i +",");
80 }
81 }
82
83 staticvoid Sample2()
84 {
85 IEnumerable<string> str = strings.Where(x=>x.StartsWith("z"));
86 foreach (string i in str)
87 {
88 Console.Write(i +",");
89 }
90 }
91
92 staticvoid Sample3()
93 {
94 var q = strings.Select(s =>new {Head=s.Substring(0,1),Tail=s.Substring(1) });
95 foreach (var p in q)
96 {
97 Console.WriteLine("Head = {0}, Tail = {1}", p.Head, p.Tail);
98 }
99 }
100
101
102 staticvoid Sample4()
103 {
104 var q = numbers.Where(x => x >5).Select(x => x==numbers[x]);
105 foreach (var i in q)
106 {
107 Console.Write(i +",");
108 }
109 }
110
111 staticvoid Sample6()
112 {
113 //int i = 0;
114 //var q = numbers.Select(x=>++i);
115 //foreach(var p in q)
116 //{
117 // Console.Write("p={0},i={1}",p,i);
118 //}
119
120
121 int i2 =0;
122 var q2 = numbers.Select(n =>++i2).ToList();
123 // The local variable i2 has already been fully incremented before we iterate the results
124 foreach (var v in q2)
125 {
126 Console.WriteLine("v = {0}, i2 = {1}", v, i2);
127 }
128 }
129
130 ///<summary>
131 /// count
132 ///</summary>
133 staticvoid Sample7()
134 {
135 var q2 = numbers.Count(x=>(x>5&&x<9));
136 Console.WriteLine("the count of number is: "+q2);
137 }
138 ///<summary>
139 /// longCount
140 ///</summary>
141 staticvoid Sample8()
142 {
143 var q2 = numbers.LongCount(x => (x >5&& x <9));
144 Console.WriteLine("the count of number is: "+ q2);
145 }
146
147 ///<summary>
148 /// Average
149 ///</summary>
150 staticvoid Sample9()
151 {
152 var q2 = numbers.ToList().Average();
153 Console.WriteLine("the Average of number is: "+ q2);
154 }
155 ///<summary>
156 /// Min
157 ///</summary>
158 staticvoid Sample10()
159 {
160 var q2 = numbers.Where(x=>x%2==0).Min();
161 Console.WriteLine("the Average of number is: "+ q2);
162 }
163
164 ///<summary>
165 /// Max
166 ///</summary>
167 staticvoid Sample11()
168 {
169 var q2 = strings.Select(x=>x.StartsWith("o")||x.StartsWith("a")).Max();
170 Console.WriteLine("the Max of string is: "+ q2);
171 }
172 ///<summary>
173 /// Sum
174 ///</summary>
175 staticvoid Sample12()
176 {
177 var q2 =numbers.Cast<int>().Cast<int>().Sum();
178 Console.WriteLine("the Sum of number is: "+ q2);
179
180 //var q3 = from c in numbers where (c > 5) select numbers.Sum();
181
182 // Console.WriteLine("the Sum of number is: " + q3);
183
184 }
185
186 ///<summary>
187 /// Concat
188 ///</summary>
189 staticvoid Sample13()
190 {
191 var q2 = product.ToList<Product>().Concat(product.ToList<Product>());
192 foreach(var i in q2)
193 {
194 Console.WriteLine("the ProductName is: "+ i.ProductName+" the id is : "+i.ProductId);
195 }
196 }
197
198 ///<summary>
199 /// ofType
200 ///</summary>
201 staticvoid Sample14()
202 {
203 var q2 = obj.OfType<string>().Where(x=>x.StartsWith("2"));
204 foreach (var i in q2)
205 {
206 Console.WriteLine("the string is: "+ i );
207 }
208 }
209
210
211
212
213 ///<summary>
214 /// ToArray
215 ///</summary>
216 staticvoid Sample15()
217 {
218 var q2 = product.ToList<Product>().Concat(product.ToList<Product>()).ToArray();
219 foreach (var i in q2)
220 {
221 Console.WriteLine("the ProductName is: "+ i.ProductName +" the id is : "+ i.ProductId);
222 }
223 }
224
225 ///<summary>
226 /// ToDictionary
227 ///</summary>
228 staticvoid Sample16()
229 {
230 var q2 = product.ToDictionary<Product,int>((x)=>(x.ProductId));
231 foreach (var i in q2)
232 {
233 Console.WriteLine("the ProductName is: "+ (i.Value as Product).ProductName +" the id is : "+ i.Key);
234 }
235 Console.ReadLine();
236 }
237
238 ///<summary>
239 /// ElementAt
240 /// 注意:索引从0开始
241 ///</summary>
242 staticvoid Sample17()
243 {
244 var q2 = product.ToList<Product>().Take(5).ElementAt(2);
245 Console.WriteLine("the ProductName is: "+ q2.ProductName);
246 }
247
248 ///<summary>
249 /// SequenceEqual 返回bool
250 ///</summary>
251 staticvoid Sample18()
252 {
253 var q2 = product.ToList<Product>().SequenceEqual(product.ToList<Product>());
254 Console.WriteLine("the SequenceEqual is: "+ q2);
255 Console.ReadLine();
256 }
257
258 ///<summary>
259 /// range,产生一个范围
260 ///</summary>
261 staticvoid Sample19()
262 {
263 string newWord =null;
264 int newWordLength =8;
265 newWord =newstring(Enumerable.Range(0, newWordLength).Select(i=>((char)('A'+i%4))).Where(i=>i!='C').ToArray());
266 Console.WriteLine("the newWord is: "+ newWord);
267 }
268
269
270 ///<summary>
271 /// Repeat
272 ///</summary>
273 staticvoid Sample20()
274 {
275 string newWord =null;
276 newWord =string.Join(string.Empty,Enumerable.Repeat("a",9).ToArray());
277 Console.WriteLine("the newWord is: "+ newWord);
278 Console.ReadLine();
279 }
280
281 ///<summary>
282 /// GroupBy
283 ///</summary>
284 staticvoid Sample21()
285 {
286 var result = from c in product
287 group c
288 by c.ProductId
289 into g
290 select new
291 {
292
293 g.Key,
294 Count=g.Count()
295 };
296 ;
297
298 foreach (var i in result)
299 {
300 Console.WriteLine("the ProductName is: "+i.Key +" the count is : "+ i.Count);
301 }
302
303
304 }
305
306
307
308 ///<summary>
309 /// jion
310 ///</summary>
311 staticvoid Sample22()
312 {
313 //var result = from o in person.ToList<Person>()
314 // from d in personDetail.ToList<PersonDetail>()
315 // where o.Id == d.Id
316 // select new
317 // {
318 // hobby = d.Hobby,
319 // groups = d.Ie
320 // };
321
322 //foreach (var obj in result)
323 //{
324 // Console.WriteLine("the Person's hobby is: " + obj.hobby);
325 // foreach (var detail in obj.groups)
326 // {
327 // Console.WriteLine("the Person's detailsProduct is: " + detail.ProductName);
328 // }
329 //}
330
331 var result = from o in person.ToList<Person>()
332 join d in personDetail.ToList<PersonDetail>()
333 on o.Id equals d.Id
334 select new
335 {
336 hobby = d.Hobby,
337 groups = d.Ie
338 };
339
340 foreach (var obj in result)
341 {
342 Console.WriteLine("the Person's hobby is: "+ obj.hobby);
343 foreach (var detail in obj.groups)
344 {
345 Console.WriteLine("the Person's detailsProduct is: "+ detail.ProductName);
346 }
347 }
348 }
349
350 ///<summary>
351 /// 嵌套查询
352 ///</summary>
353 staticvoid Sample23()
354 {
355 var result = from o in person.ToList<Person>().Where(x=>x.personDetail.Id>4)
356 select new
357 {
358 hobby=o.personDetail.Hobby,
359 groups=o.personDetail.Ie
360 };
361
362 foreach (var obj in result)
363 {
364 Console.WriteLine("the Person's hobby is: "+ obj.hobby);
365 foreach (var detail in obj.groups)
366 {
367 Console.WriteLine("the Person's detailsProduct is: "+ detail.ProductName);
368 }
369 }
370 }
371
372
373 ///<summary>
374 /// union
375 ///</summary>
376 staticvoid Sample24()
377 {
378 var result = (from c in product2 select c).Union(from d in product3 select d);
379 //or
380 //var result=from c in product2.Union(product3) select c;
381 foreach(var p in result)
382 {
383 Console.WriteLine("the union product is: "+ p.ProductName);
384
385 }
386 Console.Read();
387 }
388 }
389
390 class Person
391 {
392 publicint Id;
393 publicstring Name;
394 publicint Level;
395 public PersonDetail personDetail;
396 }
397
398 class PersonDetail
399 {
400 publicint Id;
401 publicstring Hobby;
402 public List<Product> Ie;
403 }
404
405 class Product
406 {
407 publicint ProductId;
408 publicstring ProductName;
409 }
410
411
412 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace LmbdaText
7 {
8 class Program
9 {
10 staticint[] numbers =newint[]{5,3,4,2,1,9,6,7,8,0 };
11 staticstring[] strings =newstring[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
12 staticobject[] obj={"2a",12,21.22,};
13
14
15 static Product[] product =
16 {
17 new Product{ProductId=5,ProductName="Berry"},
18 };
19 static Product[] product2 =
20 {
21 new Product{ProductId=1,ProductName="Apple"},
22 new Product{ProductId=4,ProductName="Orange"},
23 new Product{ProductId=5,ProductName="Berry"},
24 };
25 static Product[] product3 =
26 {
27
28 new Product{ProductId=4,ProductName="Orange"},
29 new Product{ProductId=5,ProductName="Berry"},
30 };
31 static Product[] product4 =
32 {
33 new Product{ProductId=1,ProductName="Apple"},
34 new Product{ProductId=2,ProductName="Banana"},
35 new Product{ProductId=3,ProductName="Grape"}
36
37 };
38 static PersonDetail[] personDetail =
39 {
40
41 new PersonDetail{Id=1, Hobby="hobby1", Ie=product.ToList<Product>()},
42 new PersonDetail{Id=3, Hobby="hobby3", Ie=product2.ToList<Product>()},
43 new PersonDetail{Id=5, Hobby="hobby5", Ie=product3.ToList<Product>()},
44 new PersonDetail{Id=7, Hobby="hobby7", Ie=product4.ToList<Product>()},
45 new PersonDetail{Id=2, Hobby="hobby2", Ie=product.ToList<Product>()},
46 };
47
48 static Person[] person =
49 {
50 new Person {Id=1,Name="Matt", Level=3,personDetail=new PersonDetail
51 { Id=1, Hobby="Hobby1",Ie=product2.ToList<Product>()},
52 },
53 new Person {Id=2,Name="Luca", Level=3,personDetail=new PersonDetail
54 { Id=2, Hobby="Hobby1",Ie=product.ToList<Product>()},},
55 new Person {Id=3,Name="Jomo", Level=5,personDetail=new PersonDetail
56 { Id=3, Hobby="Hobby1",Ie=product3.ToList<Product>()},},
57 new Person {Id=4,Name="Dinesh", Level=3,personDetail=new PersonDetail
58 { Id=4, Hobby="Hobby1",Ie=product4.ToList<Product>()},},
59 new Person {Id=5,Name="Charlie", Level=3,personDetail=new PersonDetail
60 { Id=5, Hobby="Hobby1",Ie=product2.ToList<Product>()},},
61 new Person {Id=6,Name="Mads", Level=3,personDetail=new PersonDetail
62 { Id=6, Hobby="Hobby1",Ie=product.ToList<Product>()},},
63 new Person {Id=7,Name="Anders", Level=9,personDetail=new PersonDetail
64 { Id=7, Hobby="Hobby1",Ie=product.ToList<Product>()},}
65 };
66
67 staticvoid Main(string[] args)
68 {
69 Sample24();
70
71
72 }
73
74 staticvoid Sample()
75 {
76 IEnumerable<int> nums = numbers.Where(x => x <5);
77 foreach (int i in nums)
78 {
79 Console.Write(i +",");
80 }
81 }
82
83 staticvoid Sample2()
84 {
85 IEnumerable<string> str = strings.Where(x=>x.StartsWith("z"));
86 foreach (string i in str)
87 {
88 Console.Write(i +",");
89 }
90 }
91
92 staticvoid Sample3()
93 {
94 var q = strings.Select(s =>new {Head=s.Substring(0,1),Tail=s.Substring(1) });
95 foreach (var p in q)
96 {
97 Console.WriteLine("Head = {0}, Tail = {1}", p.Head, p.Tail);
98 }
99 }
100
101
102 staticvoid Sample4()
103 {
104 var q = numbers.Where(x => x >5).Select(x => x==numbers[x]);
105 foreach (var i in q)
106 {
107 Console.Write(i +",");
108 }
109 }
110
111 staticvoid Sample6()
112 {
113 //int i = 0;
114 //var q = numbers.Select(x=>++i);
115 //foreach(var p in q)
116 //{
117 // Console.Write("p={0},i={1}",p,i);
118 //}
119
120
121 int i2 =0;
122 var q2 = numbers.Select(n =>++i2).ToList();
123 // The local variable i2 has already been fully incremented before we iterate the results
124 foreach (var v in q2)
125 {
126 Console.WriteLine("v = {0}, i2 = {1}", v, i2);
127 }
128 }
129
130 ///<summary>
131 /// count
132 ///</summary>
133 staticvoid Sample7()
134 {
135 var q2 = numbers.Count(x=>(x>5&&x<9));
136 Console.WriteLine("the count of number is: "+q2);
137 }
138 ///<summary>
139 /// longCount
140 ///</summary>
141 staticvoid Sample8()
142 {
143 var q2 = numbers.LongCount(x => (x >5&& x <9));
144 Console.WriteLine("the count of number is: "+ q2);
145 }
146
147 ///<summary>
148 /// Average
149 ///</summary>
150 staticvoid Sample9()
151 {
152 var q2 = numbers.ToList().Average();
153 Console.WriteLine("the Average of number is: "+ q2);
154 }
155 ///<summary>
156 /// Min
157 ///</summary>
158 staticvoid Sample10()
159 {
160 var q2 = numbers.Where(x=>x%2==0).Min();
161 Console.WriteLine("the Average of number is: "+ q2);
162 }
163
164 ///<summary>
165 /// Max
166 ///</summary>
167 staticvoid Sample11()
168 {
169 var q2 = strings.Select(x=>x.StartsWith("o")||x.StartsWith("a")).Max();
170 Console.WriteLine("the Max of string is: "+ q2);
171 }
172 ///<summary>
173 /// Sum
174 ///</summary>
175 staticvoid Sample12()
176 {
177 var q2 =numbers.Cast<int>().Cast<int>().Sum();
178 Console.WriteLine("the Sum of number is: "+ q2);
179
180 //var q3 = from c in numbers where (c > 5) select numbers.Sum();
181
182 // Console.WriteLine("the Sum of number is: " + q3);
183
184 }
185
186 ///<summary>
187 /// Concat
188 ///</summary>
189 staticvoid Sample13()
190 {
191 var q2 = product.ToList<Product>().Concat(product.ToList<Product>());
192 foreach(var i in q2)
193 {
194 Console.WriteLine("the ProductName is: "+ i.ProductName+" the id is : "+i.ProductId);
195 }
196 }
197
198 ///<summary>
199 /// ofType
200 ///</summary>
201 staticvoid Sample14()
202 {
203 var q2 = obj.OfType<string>().Where(x=>x.StartsWith("2"));
204 foreach (var i in q2)
205 {
206 Console.WriteLine("the string is: "+ i );
207 }
208 }
209
210
211
212
213 ///<summary>
214 /// ToArray
215 ///</summary>
216 staticvoid Sample15()
217 {
218 var q2 = product.ToList<Product>().Concat(product.ToList<Product>()).ToArray();
219 foreach (var i in q2)
220 {
221 Console.WriteLine("the ProductName is: "+ i.ProductName +" the id is : "+ i.ProductId);
222 }
223 }
224
225 ///<summary>
226 /// ToDictionary
227 ///</summary>
228 staticvoid Sample16()
229 {
230 var q2 = product.ToDictionary<Product,int>((x)=>(x.ProductId));
231 foreach (var i in q2)
232 {
233 Console.WriteLine("the ProductName is: "+ (i.Value as Product).ProductName +" the id is : "+ i.Key);
234 }
235 Console.ReadLine();
236 }
237
238 ///<summary>
239 /// ElementAt
240 /// 注意:索引从0开始
241 ///</summary>
242 staticvoid Sample17()
243 {
244 var q2 = product.ToList<Product>().Take(5).ElementAt(2);
245 Console.WriteLine("the ProductName is: "+ q2.ProductName);
246 }
247
248 ///<summary>
249 /// SequenceEqual 返回bool
250 ///</summary>
251 staticvoid Sample18()
252 {
253 var q2 = product.ToList<Product>().SequenceEqual(product.ToList<Product>());
254 Console.WriteLine("the SequenceEqual is: "+ q2);
255 Console.ReadLine();
256 }
257
258 ///<summary>
259 /// range,产生一个范围
260 ///</summary>
261 staticvoid Sample19()
262 {
263 string newWord =null;
264 int newWordLength =8;
265 newWord =newstring(Enumerable.Range(0, newWordLength).Select(i=>((char)('A'+i%4))).Where(i=>i!='C').ToArray());
266 Console.WriteLine("the newWord is: "+ newWord);
267 }
268
269
270 ///<summary>
271 /// Repeat
272 ///</summary>
273 staticvoid Sample20()
274 {
275 string newWord =null;
276 newWord =string.Join(string.Empty,Enumerable.Repeat("a",9).ToArray());
277 Console.WriteLine("the newWord is: "+ newWord);
278 Console.ReadLine();
279 }
280
281 ///<summary>
282 /// GroupBy
283 ///</summary>
284 staticvoid Sample21()
285 {
286 var result = from c in product
287 group c
288 by c.ProductId
289 into g
290 select new
291 {
292
293 g.Key,
294 Count=g.Count()
295 };
296 ;
297
298 foreach (var i in result)
299 {
300 Console.WriteLine("the ProductName is: "+i.Key +" the count is : "+ i.Count);
301 }
302
303
304 }
305
306
307
308 ///<summary>
309 /// jion
310 ///</summary>
311 staticvoid Sample22()
312 {
313 //var result = from o in person.ToList<Person>()
314 // from d in personDetail.ToList<PersonDetail>()
315 // where o.Id == d.Id
316 // select new
317 // {
318 // hobby = d.Hobby,
319 // groups = d.Ie
320 // };
321
322 //foreach (var obj in result)
323 //{
324 // Console.WriteLine("the Person's hobby is: " + obj.hobby);
325 // foreach (var detail in obj.groups)
326 // {
327 // Console.WriteLine("the Person's detailsProduct is: " + detail.ProductName);
328 // }
329 //}
330
331 var result = from o in person.ToList<Person>()
332 join d in personDetail.ToList<PersonDetail>()
333 on o.Id equals d.Id
334 select new
335 {
336 hobby = d.Hobby,
337 groups = d.Ie
338 };
339
340 foreach (var obj in result)
341 {
342 Console.WriteLine("the Person's hobby is: "+ obj.hobby);
343 foreach (var detail in obj.groups)
344 {
345 Console.WriteLine("the Person's detailsProduct is: "+ detail.ProductName);
346 }
347 }
348 }
349
350 ///<summary>
351 /// 嵌套查询
352 ///</summary>
353 staticvoid Sample23()
354 {
355 var result = from o in person.ToList<Person>().Where(x=>x.personDetail.Id>4)
356 select new
357 {
358 hobby=o.personDetail.Hobby,
359 groups=o.personDetail.Ie
360 };
361
362 foreach (var obj in result)
363 {
364 Console.WriteLine("the Person's hobby is: "+ obj.hobby);
365 foreach (var detail in obj.groups)
366 {
367 Console.WriteLine("the Person's detailsProduct is: "+ detail.ProductName);
368 }
369 }
370 }
371
372
373 ///<summary>
374 /// union
375 ///</summary>
376 staticvoid Sample24()
377 {
378 var result = (from c in product2 select c).Union(from d in product3 select d);
379 //or
380 //var result=from c in product2.Union(product3) select c;
381 foreach(var p in result)
382 {
383 Console.WriteLine("the union product is: "+ p.ProductName);
384
385 }
386 Console.Read();
387 }
388 }
389
390 class Person
391 {
392 publicint Id;
393 publicstring Name;
394 publicint Level;
395 public PersonDetail personDetail;
396 }
397
398 class PersonDetail
399 {
400 publicint Id;
401 publicstring Hobby;
402 public List<Product> Ie;
403 }
404
405 class Product
406 {
407 publicint ProductId;
408 publicstring ProductName;
409 }
410
411
412 }
以上是linq使用的一点例子和方法^^