《C#本质论》读书笔记(15)使用查询表达式的LINQ
15.1 查询表达式的概念
private static void ShowContextualKeywords1()
{
IEnumerable<string> selection = from word in Keywords
where !word.Contains('*')
select word;
foreach (string keyword in selection)
{
Console.Write(" " + keyword);
}
}
private static string[] Keywords = {
"abstract", "add*", "alias*", "as", "ascending*", "base",
"bool", "break", "by*", "byte", "case", "catch", "char",
"checked", "class", "const", "continue", "decimal",
"default", "delegate", "descending*", "do", "double",
"dynamic*", "else", "enum", "event", "equals*",
"explicit", "extern", "false", "finally", "fixed",
"from*", "float", "for", "foreach", "get*", "global*",
"group*", "goto", "if", "implicit", "in", "int",
"into*", "interface", "internal", "is", "lock", "long",
"join*", "let*", "namespace", "new", "null", "object",
"on*", "operator", "orderby*", "out", "override",
"params", "partial*", "private", "protected", "public",
"readonly", "ref", "remove*", "return", "sbyte", "sealed",
"select*", "set*", "short", "sizeof", "stackalloc",
"static", "string", "struct", "switch", "this", "throw",
"true", "try", "typeof", "uint", "ulong", "unchecked",
"unsafe", "ushort", "using", "value*", "var*", "virtual",
"void", "volatile", "where*", "while", "yield*"
};

15.1.1 投射
IEnumerbale<T>
或 IQueryable<T>
集合。T数据类型是从select或者groupby子句推导。select word
推导的,因为word是一个字符串。word数据类型是由from
子句所指定的IEnumerbale<T>集合的类型参数(这里是Keywords)。由于Keywords是一个string数组,它实现了IEnumerbale<T>
,所以word是一个字符串。public static void Main()
{
List1(Directory.GetCurrentDirectory(),"*");
}
static void List1(string rootDirectory, string searchPattern)
{
IEnumerable<FileInfo> files =
from fileName in Directory.GetFiles(
rootDirectory, searchPattern)
select new FileInfo(fileName);
foreach (FileInfo file in files)
{
Console.WriteLine(".{0} ({1})",
file.Name, file.LastWriteTime);
}
}

IEnumerable<FileInfo>
,而不是System.IO.Directory.GetFiles()
返回的IEnumerables<string>
数据类型。
var files =
from fileName in Directory.GetFiles(
rootDirectory, searchPattern)
select new FileInfo(fileName);
15.1.2 筛选
IEnumerable<string> selection = from word in Keywords
where !word.Contains('*')
select word;
15.1.3 排序
IEnumerable<string> fileNames =
from fileName in Directory.GetFiles(
rootDirectory, searchPattern)
orderby (new FileInfo(fileName)).Length descending,
fileName
select fileName;
15.1.4 let子句
public static void Main()
{
ListByFileSize2(Directory.GetCurrentDirectory(), "*");
}
static void ListByFileSize2(
string rootDirectory, string searchPattern)
{
IEnumerable<FileInfo> files =
from fileName in Directory.GetFiles(
rootDirectory, searchPattern)
orderby new FileInfo(fileName).Length, fileName
select new FileInfo(fileName);
foreach (FileInfo file in files)
{
// As simplification, current directory is
// assumed to be a subdirectory of
// rootDirectory
string relativePath = file.FullName.Substring(
Environment.CurrentDirectory.Length);
Console.WriteLine(".{0}({1})",
relativePath, file.Length);
}
}
IEnumerable<FileInfo> files =
from fileName in Directory.GetFiles(
rootDirectory, searchPattern)
let file = new FileInfo(fileName)
orderby file.Length, fileName
select file;
let子句引入了一个新的范围变量
它容纳的表达式值可以在查询表达式剩余部分使用
可以添加任意数量的let表达式,只需要它们每一个作为一个附加的子句
放在第一个from子句之后,最后一个select/group by子句之前,加入查询即可
15.1.5 分组
private static void GroupKeywords1()
{
IEnumerable<IGrouping<bool, string>> selection =
from word in keyWords
group word by word.Contains('*');
foreach (IGrouping<bool, string> wordGroup
in selection)
{
Console.WriteLine(Environment.NewLine + "{0}:",
wordGroup.Key ?
"Contextual Keywords" : "Keywords");
foreach (string keyword in wordGroup)
{
Console.Write(" " +
(wordGroup.Key ?
keyword.Replace("*", null) : keyword));
}
}
}

结果


private static void GroupKeywords1()
{
IEnumerable<IGrouping<bool, string>> keywordGroups =
from word in keyWords
group word by word.Contains('*');
var selection = from groups in keywordGroups
select new
{
IsContextualKeyword = groups.Key,
Items = groups
};
foreach (var wordGroup in selection)
{
Console.WriteLine(Environment.NewLine + "{0}:",
wordGroup.IsContextualKeyword ?
"Contextual Keywords" : "Keywords");
foreach (var keyword in wordGroup.Items)
{
Console.Write(" " +
keyword.Replace("*", null));
}
}
}

15.1.6 使用into进行查询延续
var selection =
from word in keyWords
group word by word.Contains('*')
into groups
select new
{
IsContextualKeyword = groups.Key,
Items = groups
};
15.1.7 用多个from 子句“平整”序列的序列
var selection = from word in keyWords
from character in word
select character;

var numbers = new[] {1, 2, 3};
var product = from word in keyWords
from number in numbers
select new {word, number};

主题:不重复的成员

15.2 查询表达式作为方法调用
IEnumerable<string> selection = from word in Keywords
where !word.Contains('*')
select word;
IEnumerable<string> selection =
keyWords.Where(word => word.Contains('*'));
《C#本质论》读书笔记(15)使用查询表达式的LINQ的更多相关文章
- C#复习笔记(4)--C#3:革新写代码的方式(查询表达式和LINQ to object(下))
查询表达式和LINQ to object(下) 接下来我们要研究的大部分都会涉及到透明标识符 let子句和透明标识符 let子句不过是引入了一个新的范围变量.他的值是基于其他范围变量的.let 标识符 ...
- 十五、C# 使用查询表达式的LINQ
使用查询表达式的LINQ 本章介绍了一种新的语法,查询表达式. 1.查询表达式概述 2.特点:投射 筛选 排序 Let 分组 3.作为方法调用 标准查询运算符所实现的查询在功能上 ...
- 查询表达式和LINQ to Objects
查询表达式实际上是由编译器“预处理”为“普通”的C#代码,接着以完全普通的方式进行编译.这种巧妙的发式将查询集合到了语言中,而无须把语义改得乱七八糟 LINQ的介绍 LINQ中的基础概念 降低两种数据 ...
- 『TCP/IP详解——卷一:协议』读书笔记——15
2013-08-25 13:39:40 第6章 ICMP:Internet控制报文协议 6.1 引言 ICMP经常被认为是IP层的一个组成部分.它传递差错报文以及其他需要注意的信息.ICMP报文同通常 ...
- Microsoft SqlServer2008技术内幕:T-Sql语言基础-读书笔记-单表查询SELECT语句元素
1.select语句逻辑处理顺序: FORM WHERE GROUP BY HAVING SELECT OVER DISTINCT TOP ORDER BY 总结: 2.FORM子句的表名称应该带上数 ...
- C#本质论读书笔记:第一章 C#概述|第二章 数据类型
第一章 1.字符串是不可变的:所有string类型的数据,都不可变,也可以说是不可修改的,不能修改变量最初引用的数据,只能对其重新赋值,让其指向内存中的一个新位置. 第二章 2.1 预定义类型或基本类 ...
- Java SE 8 for the Really Impatient读书笔记——Java 8 Lambda表达式
1. lambda表达式的语法 lambda表达式是一种没有名字的函数,它拥有函数体和参数. lambda表达式的语法十分简单:参数->主体.通过->来分离参数和主体. 1.1 参数 la ...
- Http读书笔记1-5章
第一章 内容提要 这一章主要介绍了什么是http以及http是干嘛的,以及与之有关的相关概念,当然了这些概念都是概览式的介绍一些.所以我将采用问答式的方式描述这一章! Q:http是干嘛的? A:ht ...
- SQL.Cookbook 读书笔记5 元数据查询
第五章 元数据查询 查询数据库本身信息 表结构 索引等 5.1 查询test库下的所有表信息 MYSQL SELECT * from information_schema.`TABLES` WHERE ...
随机推荐
- Thinking in java学习笔记之final
- wpf *和auto的区别
Auto 表示自动适应显示内容的宽度, 如自动适应文本的宽度,文本有多长,控件就显示多长. * 则表示按比例来分配宽度. <ColumnDefinition Width="3*&quo ...
- HDU 2204Eddy's爱好(容斥原理)
Eddy's爱好 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- C#之方法的声明与调用
//params关键字使用 class Program { static void Main(string[] args){ , , , }; Console.WriteLine(AddFunctio ...
- Root--超级用户
http://www.shuame.com/root/ root (计算机术语言) ROOT存在于Linux系统.UNIX系统(如AIX.BSD等)和类UNIX系统(如稳定到服务器都在用的Debia ...
- CentOS Linux解决Device eth0 does not seem to be present
在VMware里克隆出来的Centos Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: 解决办法: 首先,打开/etc/udev/rules.d/70 ...
- sqlpuls基本命令
1.直接敲sqlplus并回车就是启动SQL*PLUS,输入user及password将使用户登陆到缺省的数据库.2.sqlplus user/password@SERVICE_NAME 将连接到指定 ...
- PAT MOOC dataStructure 4-1
数据结构练习 4-1 AVL 树 1. 题目: Input Specification: Each input file contains one test case. For each case, ...
- JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 【C语言入门教程】目录/大纲
第一章 C语言编程基础 1.1 基本程序结构 1.2 函数库 和 链接 1.3 C语言“32个”关键字 第二章 数据类型.运算符和表达式 2.1 数据类型(5种基本数据类型),聚合类型与修饰符 2.2 ...