Linq101-Conversion Operators

 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 }

 

转载于:https://www.cnblogs.com/David-Huang/p/4256255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值