码迷,mamicode.com
首页 > 其他好文 > 详细

Linq101-Conversion Operators

时间:2015-01-28 17:37:00      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 using System.Linq;
 3 
 4 namespace Linq101
 5 {
 6     class Conversion
 7     {
 8         /// <summary>
 9         /// This sample uses ToArray to immediately evaluate a sequence into an array.
10         /// </summary>
11         public void Linq54()
12         {
13             double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
14 
15             //var doublesArray = doubles.OrderByDescending(d => d).ToArray();
16             var sortedDoubles = from d in doubles
17                                 orderby d descending
18                                 select d;
19             var doublesArray = sortedDoubles.ToArray();
20 
21             Console.WriteLine("Every other double from highest to lowest:");
22             for (int d = 0; d < doublesArray.Length; d += 2)
23             {
24                 Console.WriteLine(doublesArray[d]);
25             }
26         }
27 
28         /// <summary>
29         /// This sample uses ToList to immediately evaluate a sequence into a List<T>.
30         /// </summary>
31         public void Linq55()
32         {
33             string[] words = { "cherry", "apple", "blueberry" };
34 
35             //var wordList = words.OrderBy(w => w).ToList();
36             var sortedWords =
37                 from w in words
38                 orderby w
39                 select w;
40             var wordList = sortedWords.ToList();
41 
42             Console.WriteLine("The sorted word list:");
43             foreach (var w in wordList)
44             {
45                 Console.WriteLine(w);
46             }
47         }
48 
49         /// <summary>
50         /// This sample uses ToDictionary to immediately evaluate a sequence and a related key expression into a dictionary.
51         /// </summary>
52         public void Linq56()
53         {
54             var scoreRecords = new[] { new {Name = "Alice", Score = 50}, 
55                                 new {Name = "Bob"  , Score = 40}, 
56                                 new {Name = "Cathy", Score = 45} 
57                             };
58 
59             var scoreRecordsDict = scoreRecords.ToDictionary(s => s.Name);
60 
61             Console.WriteLine("Bob‘ Score: {0}", scoreRecordsDict["Bob"].Score);
62         }
63 
64         /// <summary>
65         /// This sample uses OfType to return only the elements of the array that are of type double.
66         /// </summary>
67         public void Linq57()
68         {
69             object[] numbers = { null, 1.0, "two", 3, "four", 5, "six", 7.0 };
70 
71             var doubles = numbers.OfType<double>();
72 
73             Console.WriteLine("Numbers stored as doubles:");
74             foreach (var d in doubles)
75             {
76                 Console.WriteLine(d);
77             }
78         }
79     }
80 }

 

Linq101-Conversion Operators

标签:

原文地址:http://www.cnblogs.com/David-Huang/p/4256255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!