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 ...
随机推荐
- 【已解决】WebUploader 0.1.5 安卓手机不能访问相机、IOS直接访问相机 的问题
WebUploader 0.1.5 安卓手机不能访问相机.IOS直接访问相机 的问题 打开 webuploader.js if(navigator.userAgent.indexOf('Android ...
- 捕获网络数据包并进行分析的开源库-WinPcap
什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 大多数网络应用程序通过被广泛使用的操作系统元件来访问网络,比如sockets. 这是一种简单的 ...
- hdu 4112 Break the Chocolate(乱搞题)
题意:要把一块n*m*k的巧克力分成1*1*1的单元,有两种操作方式:1,用手掰(假设力量无穷大),每次拿起一块,掰成两块小的:2,用刀切(假设刀无限长),可以把多块摆在一起,同时切开.问两种方式各需 ...
- window 添加环境变量
右击我的电脑 选择属性 点选高级选项卡 点击环境变量 在系统变量中选中path变量 点击编辑 在变量值得最后插入 ;C:\Python27\ (改为Python的实际的安装地址) 记住后面要有最后面 ...
- 【SQLServer2008】之如何导入mdf,ldf文件、导入.mdf文件报错解决问题以及如何获得Authenticated Users 权限
一.如何导入mdf,ldf文件: 链接:http://jingyan.baidu.com/article/09ea3ede21258cc0afde3943.html 二.导入.mdf文件报错解决问题 ...
- win10正式版开始菜单无法打开,右边的网络连接、操作中心也打不开
问题描述: 开机后电脑键盘的win键无响应,鼠标点击菜单栏中的这几个按键也都无响应,但是点击自己固定的应用程序却没有问题,在网上查找尝试了许多资料,终于找到了一个合适的解决方案.现记录如下 解决方案: ...
- label 标签的用法,点label选中单选、复选框或文本框
<label>拥有的权限</label> <label class="checkbox" id="privilege_id" st ...
- Matlab典型论坛
Matlab典型论坛 http://www.ilovematlab.cn/forum.php?tid=271705&goto=lastpost
- 常见UI组件的一个模板
效果: 代码: using UnityEngine; using UnityEditor; using System.Collections; using System.IO; using Unity ...
- .net Socket编程
1. 什么是TCP/IP.UDP?2. Socket在哪里呢?3. Socket是什么呢?4. 你会使用它们吗? 什么是TCP/IP.U ...