LINQ 学习路程 -- 查询语法 LINQ Query Syntax
1.查询语法
Query Syntax:
from <range variable> in <IEnumerable<T> or IQueryable<T> Collection> <Standard Query Operators> <lambda expression> <select or groupBy operator> <result formation>
// string collection
IList<string> stringList = new List<string>() {
"C# Tutorials",
"VB.NET Tutorials",
"Learn C++",
"MVC Tutorials" ,
"Java"
}; // LINQ Query Syntax
var result = from s in stringList
where s.Contains("Tutorials")
select s;

查询语法以From开头,后面紧跟着Range veriable变量,From从句像这样的结构"From rangeVariableName in IEnumerablecollection",意思是从集合的每个对象中获取,它有点像foreach循环,
foreach(Student s in studentList)
在From从句之后,我们可以使用不同的标准查询运算符来过滤、分类和联合集合里面的元素,大概有50个标准查询操作
一般以Select 或Group从句结尾,Select从句用来构建数据,你可以查询全部对象或者它的几个属性。
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
};
// LINQ Query Syntax to find out teenager students
var teenAgerStudent = from s in studentList
where s.Age > && s.Age <
select s;
LINQ Method Syntax
// string collection
IList<string> stringList = new List<string>() {
"C# Tutorials",
"VB.NET Tutorials",
"Learn C++",
"MVC Tutorials" ,
"Java"
}; // LINQ Query Syntax
var result = stringList.Where(s => s.Contains("Tutorials"));


// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; // LINQ Method Syntax to find out teenager students
var teenAgerStudents = studentList.Where(s => s.Age > && s.Age < )
.ToList<Student>();
Standard Query Operators:
Standard Query Operators in Query Syntax:

Standard Query Operators in Method Syntax:

标准查询操作根据功能分类
| Classification | Standard Query Operators |
|---|---|
| Filtering(筛选) | Where, OfType |
| Sorting(排序) | OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse |
| Grouping(分组) | GroupBy, ToLookup |
| Join(连接) | GroupJoin, Join |
| Projection(投影) | Select, SelectMany |
| Aggregation(聚集) | Aggregate, Average, Count, LongCount, Max, Min, Sum |
| Quantifiers(量词) | All, Any, Contains |
| Elements(元素) | ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault |
| Set(集合) | Distinct, Except, Intersect, Union |
| Partitioning(分割) | Skip, SkipWhile, Take, TakeWhile |
| Concatenation(连接) | Concat |
| Equality | SequenceEqual |
| Generation | DefaultEmpty, Empty, Range, Repeat |
| Conversion | AsEnumerable, AsQueryable, Cast, ToArray, ToDictionary, ToList |
LINQ 学习路程 -- 查询语法 LINQ Query Syntax的更多相关文章
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Conversion Operators
Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
随机推荐
- brew Error: Formulae found in multiple taps
Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...
- Python基础之函数与装饰器
阅读目录 一.为什么要使用函数 二.函数的定义与调用 三.函数返回值 四.函数的参数 五.本章小结 六.装饰器 一.函数流程图: 函数名的命名规则: 1.函数名必须由字母下划线数字组成,不能是关键字和 ...
- 匿名函数块级作用域以及在JQuery中的应用
最近经常回在群里面看到有些朋友问如下这样的js写法该如何理解的的问题,虽然可能对匿名函数有些理解,但是有时候看到JQuery源码或者其他JS库中同样的写法时,就有点不理解了,今天再次分享下这方面的知识 ...
- es6/es7 对象数组的合并拷贝
方法一: let o1 = { a: 1, b: 2, c: 3 }; let o2 = {...o1, d: 4}; // o2 = { a: 1, b: 2, c: 3, d: 4 } let a ...
- Google 全球 IP 地址库
## Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93. ...
- cp 命令 简要
1.cp 1.log hanDir 将1.log复制到hanDir目录中 2.cp 1.log -i hanDir 复制前询问是否覆盖重名文件 3.cp -a han ...
- Jenkins入门(一)
Jenkins就是一个Java Web应用,它主要是干什么呢? 其实很简单: 下载一个jenkins的war包,然后扔到tomcat 的webapps中,启动这个tomcat,访问jenkins应用即 ...
- delphi 无边框窗体常见问题
实现无边框窗体很简单,直接将窗体的BorderStyle属性设置为bsNone即可.但是这样会引起2个问题: 1.在xp系统下,任务栏鼠标右键点击无法弹出菜单 解决办法:在FormShow中加入这个过 ...
- 关于js语法中的一些难点(预解析,变量提前,作用域)
******标题很吓人************ 其实就是一个小小的例子 ,从例子中简单的分析一下作用域.预解析和变量提前的概念 <!DOCTYPE html> <html> & ...
- 我为什么要学习jquery
因为jquery很强大,很少的代码实现更多的功能.