zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    编译redis时 提示make cc Command not found
    【netty】Netty系列之Netty百万级推送服务设计要点
    [nginx]Nginx禁止访问robots.txt防泄漏web目录
    【linux】 linux 查看系统信息
    [spring] java.lang.reflect.MalformedParameterizedTypeException
    【spring】non-compatible bean definition of same name and class
    【java】serialVersionUID作用
    【架构】生成全局唯一ID的3个思路,来自一个资深架构师的总结
    [linux]BASH 的基本语法
    [linux] linux 破解版confluence安装
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4256255.html
Copyright © 2011-2022 走看看