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

最新文章

  1. 如何在github中创建演示demo
  2. PSP记录个人项目耗时情况
  3. 自己不懂的SQL语句用法
  4. PHP中对淘宝URL中ID提取
  5. Atitit.在线充值功能的设计
  6. EB(存储单位)
  7. linux命令——磁盘命令mkdir
  8. ajax提交富文本,内容被截断,解决方法及思路
  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历
  10. PHP获取当前的毫秒值
  11. python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
  12. bibli直播弹幕实时爬取
  13. bee: command not found问题解决之道
  14. Apache Kafka 快速入门
  15. battery for stm32
  16. SQL Server清理大日志文件方法 不分离数据库 执行SQL语句即可
  17. webstorm去掉vue错误提示
  18. win7 开机自启动控制
  19. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
  20. Vim-Go环境搭建

热门文章

  1. 努比亚(nubia) Z18 mini NX611J 解锁BootLoader 并刷入recovery ROOT
  2. ibatis知识点
  3. JS——缓慢动画封装案例
  4. [Windows Server 2008] 查看ASP.net详细错误信息
  5. Java_Web三大框架之Hibernate操作数据库(三)
  6. Mongo优化笔记
  7. Scroll / Jump to id without jQuery
  8. Origin C调用GSL
  9. ES6 中set的用法
  10. CSS超链接常见四种状态在控制