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 }
时间: 2024-10-11 07:56:12

Linq101-Conversion Operators的相关文章

101个LINQ例子

Restriction Operators Where - Simple 1 public void Linq1() {    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };    var lowNums =        from n in numbers        where n < 5        select n;    Console.WriteLine("Numbers < 5:");    foreac

101个LINQ示例,包含几乎全部操作

Restriction Operators Where - Simple 1 public void Linq1() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Console.WriteLine("Numbers < 5:"); foreach (var x in lowNums) { Console.Wr

coffeescript 1.8.0 documents

CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule

Data Types

原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administration Data Types Each value manipulated by Oracle Database has a data type. The data type of a value associates a fixed set of properties with the va

LINQ Sample

Sample LINQ Queries: In this section, you will learn some complex LINQ queries. We will use the following Student and Standard collection for our queries. Sample Collections: IList<Student> studentList = new List<Student>() { new Student() { S

c++几个新特性

template 模板 1.出于通用性考虑,程序库中几乎所有东西都被设计为template形式,不支持template几乎不能使用标准程序库. 2.所谓template,是针对"一个或多个尚未明确的类型"所撰写的函数或类.使用template时,可以显示地(explicitly)或隐式地(implicitly)将类型当作参数传递. 3.目前唯一能让"template的运用"具有可移植的方式,就是在头文件中以inline function实现template func

C++ Programming language读书笔记

http://blog.163.com/leonary_dy/blog/static/405528602009122103416862/ 虽然很多人一再强调语言细节不重要,可我还是要花时间重读经典.上次我认认真真读这本书还要追溯到两年前,现在我对C++的理解更深了一层,可以从书中读到一些两年前无法领悟的东西. 声明:本笔记只记录我以前不了解的部分,请初学者不要作为书籍摘要 我看的是TCPL电子版第三版,June 1997第一次印刷.这是网上最常见的版本,前两页书皮虽然写着special版,其实后

C++ 重载操作符与转换

<C++ Primer 4th>读书笔记 重载操作符是具有特殊名称的函数:保留字 operator 后接需定义的操作符号. Sales_item operator+(const Sales_item&, const Sales_item&); 声明了加号操作符,可用于将两个 Sales_item 对象“相加”并获得一个 Sales_item 对象的副本. 不能重载的操作符 :: .* . ?: 通过连接其他合法符号可以创建新的操作符.例如,定义一个 operator**以提供求

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and

c++11介绍

C++11标准是 ISO/IEC 14882:2011 - Information technology -- Programming languages -- C++ 的简称[1]  . C++11标准由国际标准化组织(ISO)和国际电工委员会(IEC)旗下的C++标准委员会(ISO/IEC JTC1/SC22/WG21)于2011年8月12日公布[2]  ,并于2011年9月出版.2012年2月28日的国际标准草案(N3376)是最接近于C++11标准的草案(仅编辑上的修正).此次标准为C+