https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities能力 directly into the C# language (also in Visual Basic and potentially any other .NET language). With LINQ, a query is now a first-class language construct, just like classes, methods, events and so on.

For a developer who writes queries, the most visible "language-integrated" part of LINQ is the query expression.

Query expressions are written in a declarative陈述的,说明的 query syntax introduced in C# 3.0.

By using query syntax, you can perform even complex filtering, ordering, and grouping operations on data sources with a minimum of code.

You use the same basic query expression patterns to query and transform data in SQL databases, ADO.NET Datasets, XML documents and streams, and .NET collections.

The following example shows the complete query operation.

The complete operation includes creating a data source, defining the query expression, and executing the query in a foreach statement.

                // Specify the data source.
int[] scores = new int[] { , , , }; // Define the query expression.
IEnumerable<int> scoreQuery =
from score in scores
where score >
select score; // Execute the query.
foreach(int i in scoreQuery)
{
Console.Write(i + " ");
}

For more information about the basics of LINQ in C#, see Getting Started with LINQ in C#.

Query Expression Overview

 
  • Query expressions can be used to query and to transform data from any LINQ-enabled data source. For example, a single query can retrieve data from a SQL database, and produce an XML stream as output.

  • Query expressions are easy to master because they use many familiar C# language constructs. For more information, see Getting Started with LINQ in C#.

  • The variables in a query expression are all strongly typed, although in many cases you do not have to provide the type explicitly明确地 because the compiler can infer推断 it. For more information, see Type Relationships in LINQ Query Operations (C#).

  • A query is not executed until you iterate over the query variable in a foreach statement. For more information, see Introduction to LINQ Queries (C#).

  • At compile time, query expressions are converted to Standard Query Operator method calls according to the rules set forth in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax. However, in most cases query syntax is more readable and concise简洁的. For more information, see C# Language Specification and Standard Query Operators Overview.

  • As a rule when you write LINQ queries, we recommend that you use query syntax whenever possible and method syntax whenever necessary. There is no semantic语义的 or performance difference between the two different forms. Query expressions are often more readable than equivalent expressions written in method syntax.

  • Some query operations, such as Count or Max, have no equivalent query expression clause子句 and must therefore因此 be expressed as a method call. Method syntax can be combined with query syntax in various ways. For more information, see LINQ Query Syntax versus Method Syntax (C#).

  • Query expressions can be compiled to expression trees or to delegates, depending on the type that the query is applied to.IEnumerable<T> queries are compiled to delegates. IQueryable and IQueryable<T> queries are compiled to expression trees. For more information, see Expression Trees (C# and Visual Basic).

The following table lists topics that provide additional information about queries and code examples for common tasks.

Topic

Description

Query Expression Basics (C# Programming Guide)

Introduces fundamental query concepts and provides examples of C# query syntax.

How to: Write LINQ Queries in C#

Provides examples of several basic types of query expressions.

How to: Handle Exceptions in Query Expressions (C# Programming Guide)

How and when to move potential exception-throwing code outside a query expression.

How to: Populate Object Collections from Multiple Sources (LINQ)

How to use the select statement to merge data from different sources into a new type.

How to: Group Query Results (C# Programming Guide)

Shows different ways to use the group clause.

How to: Create a Nested Group (C# Programming Guide)

Shows how to create nested groups.

How to: Perform a Subquery on a Grouping Operation (C# Programming Guide)

Shows how to use a sub-expression in a query as a data source for a new query.

How to: Group Results by Contiguous Keys (C# Programming Guide)

Shows how to implement a thread-safe standard query operator that can perform grouping operations on streaming data sources.

How to: Dynamically Specify Predicate Filters at Runtime (C# Programming Guide)

Shows how to supply an arbitrary number of values to use in equality comparisons in awhere clause.

How to: Store the Results of a Query in Memory (C# Programming Guide)

Illustrates how to materialize and store query results without necessarily using aforeach loop.

How to: Return a Query from a Method (C# Programming Guide)

Shows how to return query variables from methods, and how to pass them to methods as input parameters.

How to: Perform Custom Join Operations (C# Programming Guide)

Shows how to perform join operations based on any kind of predicate function.

How to: Join by Using Composite Keys (C# Programming Guide)

Shows how to join two sources based on more than one matching key.

How to: Order the Results of a Join Clause (C# Programming Guide)

Shows how to order a sequence that is produced by a join operation.

How to: Perform Inner Joins (C# Programming Guide)

Shows how to perform an inner join in LINQ.

How to: Perform Grouped Joins (C# Programming Guide)

Shows how to produce a grouped join in LINQ.

How to: Perform Left Outer Joins (C# Programming Guide)

Shows how to produce a left outer join in LINQ.

How to: Handle Null Values in Query Expressions (C# Programming Guide)

Shows how to handle null

LINQ Query Expressions的更多相关文章

  1. Linq Query常见错误

    1.只能对 Type.IsGenericParameter 为 True 的类型调用方法 对于此错误,一般常见在虚拟实体,但是要把条件拼接在Expression中,通常是因为该字段在数据库中是可空的, ...

  2. LINQ 学习路程 -- 查询语法 LINQ Query Syntax

    1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> ...

  3. linq query, using int.parse to convert varchar to int while orderby

    var t = from x in context.NewsLetterItem.ToList() //add .ToList at this place where x.APPId == appid ...

  4. Linq to Sharepoint--如何获取Linq Query 生成的CALM

    我们知道Linq to sharepoint 实际最终还是转化成了CALM来对Sharepoint进行访问,那么我们怎样才能知道我们编写的Query语句最终转化成的CALM语句是什么样子呢. 我们可以 ...

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

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

  6. 对于Linq关键字和await,async异步关键字的扩展使用

    最近在看neuecc大佬写的一些库:https://neuecc.medium.com/,其中对await,async以及linq一些关键字实现了自定义化使用, 使其不需要引用对应命名空间,不需要多线 ...

  7. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

  8. Part 99 Lambda expression in c#

    class Program { static void Main(string[] args) { List<Person> persons = new List<Person> ...

  9. String.Join Method

    Overloads Join(String, String[], Int32, Int32) Concatenates the specified elements of a string array ...

随机推荐

  1. 增加删除div

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  2. Oracle Sequence不设置cache参数的几个潜在问题(转载)

    转载于 http://www.uml.org.cn/sjjm/201204065.asp 在Oracle中,我们没有MYSQL和SQL                           Server ...

  3. WCF开发的流程-服务端和客户端之间的通讯(内含demo讲解)

    讲解技术之前,恳请博友让我说几句废话.今天是我第一在博客园发布属于自己原创的博文(如有雷同,那是绝对不可能的事,嘿嘿).之前一直是拜读各位博友的大作,受益匪浅的我在这对博友们说声谢谢,谢谢你们的共享! ...

  4. SolidWorks学习笔记(一)

    一.草图绘制 1.简单命令 先直线后圆弧,几何约束.尺寸标定 2.圆周阵列 3.几何关系——对称 添加几何约束:圆弧相等.关于竖直中心线的对称 4.捕捉圆心 5.尺寸锁定,有圆弧的,先直后圆 6. 7 ...

  5. 利用open MP获取计算机核心数量的方法

    openMP是一款普遍通用的并行计算编程模型,使用它通常能够充分利用多核计算的优势. 以下是一种能够测试核心数量的方法: std::cout << "parallel begin ...

  6. jdk编译安装及tomcat编译安装

    这里我安装的jdk版本为1.8版本,tomcat版本为8.5(请上官网下载) 运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) jdk部署: 1.前往软件所在路径 [root@web ...

  7. nagios新增监控集群、卸载监控集群批量操作

    1.一定要找应用侧确认每台节点上需要监控的进程,不要盲目以为所有hadoop集群的zk.journal啥的都一样,切记! 2.被监控节点只需要安装nagios-plugin和nrpe,依赖需要安装xi ...

  8. [bzoj3207]花神的嘲讽计划Ⅰ[可持久化线段树,hash]

    将每k个数字求一个哈希值,存入可持久化线段树,直接查询即可 #include <iostream> #include <algorithm> #include <cstd ...

  9. 积木大赛 2013年NOIP全国联赛提高组

    题目描述 Description 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第i块积木的最终高度需要是hi.在搭建开 ...

  10. MyBatis 3实现时间段精确的查询(转)

    效果如下: 说明: 时间范围的查询会存在以下问题: 1.如果单纯采用年月日的形式会出现缺少最后一点的数据,比如要查询2015-09-16到2015-09-17,那么2015-09-17 01:00:0 ...