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的更多相关文章

  1. LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...

  2. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  3. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  4. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  5. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  6. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  7. LINQ 学习路程 -- 查询操作 Conversion Operators

    Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...

  8. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...

  9. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

随机推荐

  1. 第八章 委托,lamdbda 表达式和事件

    第八章 委托,lamdbda 表达式和事件 委托是寻址方式的.net版本. 委托是类型安全的类,它定义了返回类型和参数的类型.委托类不仅包含方法的应用,也可以包含对多个方法的引用. 在 C++中,函数 ...

  2. 我的_vimrc文件

    """"""""""""""""&quo ...

  3. swift - 全屏pop手势

    UINavigationController系统自带有侧滑手势,但是这个手势第一点只能边缘侧滑才可以有效,第二点当手动隐藏系统的导航时,这个手势就不能生效了 为了能到达到全屏pop的效果这里有2中解决 ...

  4. Eclipse注释模板设置

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  5. 《Python基础教程》第20章学习笔记

    python实现:https://github.com/captainwong/instant_markup c++实现:https://github.com/captainwong/instant_ ...

  6. 【windows7 + Appium】之Appium安装以及其他工具安装配置

    首先感谢虫师总结的教程:<appium新手入门>.以及:<appium新手入门(2)—— 安装 Android SDK> 目录: 安装Appium&安装node.js ...

  7. 【Mysql】之视图操作

    一.视图实例1-创建视图及查询数据操作 首先,创建三个表:user.course.user_course 表:user CREATE TABLE `user` ( `id` ) NOT NULL AU ...

  8. 【WPF学习笔记】之依赖属性

    概述: Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能.这些服务通常统称为 WPF 属性系统.由 ...

  9. ActivityLifecycleCallbacks 如何控制activity的生命周期

    Android开发 - ActivityLifecycleCallbacks使用方法初探 初识 ActivityLifecycleCallbacks 利用ActivityLifecycleCallba ...

  10. JVM调优- 学习笔记(转)

    http://blog.csdn.net/fenglibing/article/details/6321453 GC学习笔记 这是我公司同事的GC学习笔记,写得蛮详细的,由浅入深,循序渐进,让人一看就 ...