• 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 }
  • 相关阅读:
    CNZZ公告:近期无法获取百度关键词
    怎样注册uber司机 如何注册uber司机 最新详细攻略
    Uber司机一周体验记:成单率仅57%
    Uber司机手机终端问答篇
    Uber 司机有话说:你以为当个 Uber 司机很轻松?大错特错!
    每门编程语言修复了什么
    let区别(关于racket和r5rs)
    Bloom Filter
    静态作用域与动态作用域
    C 语言的可变参数表函数的设计
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4256255.html
Copyright © 2020-2023  润新知