LINQ Query Expressions
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 |
---|---|
Introduces fundamental query concepts and provides examples of C# query syntax. |
|
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. |
Shows different ways to use the group clause. |
|
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. |
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. |
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. |
Shows how to perform an inner join in LINQ. |
|
Shows how to produce a grouped join in LINQ. |
|
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的更多相关文章
- Linq Query常见错误
1.只能对 Type.IsGenericParameter 为 True 的类型调用方法 对于此错误,一般常见在虚拟实体,但是要把条件拼接在Expression中,通常是因为该字段在数据库中是可空的, ...
- LINQ 学习路程 -- 查询语法 LINQ Query Syntax
1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> ...
- 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 ...
- Linq to Sharepoint--如何获取Linq Query 生成的CALM
我们知道Linq to sharepoint 实际最终还是转化成了CALM来对Sharepoint进行访问,那么我们怎样才能知道我们编写的Query语句最终转化成的CALM语句是什么样子呢. 我们可以 ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- 对于Linq关键字和await,async异步关键字的扩展使用
最近在看neuecc大佬写的一些库:https://neuecc.medium.com/,其中对await,async以及linq一些关键字实现了自定义化使用, 使其不需要引用对应命名空间,不需要多线 ...
- SQL Queries from Transactional Plugin Pipeline
Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...
- Part 99 Lambda expression in c#
class Program { static void Main(string[] args) { List<Person> persons = new List<Person> ...
- String.Join Method
Overloads Join(String, String[], Int32, Int32) Concatenates the specified elements of a string array ...
随机推荐
- Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)
1.配置php.ini vi /etc/php.ini 2.配置apache 先给需要配置的文件做个备份 cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/h ...
- 软件架构自学笔记-- 转载“虎牙在全球 DNS 秒级生效上的实践”
虎牙在全球 DNS 秒级生效上的实践 这次分享的是全球 DNS 秒级生效在虎牙的实践,以及由此产生的一些思考,整体上,分为以下 5 各部分: 背景介绍: 方案设计和对比: 高可用: 具体实践和落地: ...
- Python之IPython开发实践
Python之IPython开发实践 1. IPython有行号. 2. Tab键自动完成,当前命名空间任何与已输入字符串相匹配的变量就会被找出来. 3. 内省机制,在变量前或者后面加上(?)问号,就 ...
- 努比亚(nubia) Z18 mini NX611J 解锁BootLoader 并刷入recovery ROOT
努比亚(nubia) Z18 mini NX611J解锁BootLoader 并刷入recovery ROOT 工具下载链接:https://pan.baidu.com/s/1toU-mTR9FNE ...
- JS高级——Function原型链
基本概念 1.函数可以通过Function new出来,那么Function可以被称作构造函数,被new出来的函数可以被称为一个对象 2.Function既然是构造函数,那么肯定也有原型,它的原型是一 ...
- [Windows Server 2008] Ecshop安全设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:ECSHO ...
- 离线安装Selenium
https://blog.csdn.net/poem_ruru/article/details/79032140
- java_第一个servlet小程序
xml中注册: <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>s ...
- js >> 右移操作符
十进制 十六进制 二进制 右移>> 十进制值 F 1F FF
- 换个语言学一下 Golang (4)——变量与常量
一.变量定义 所谓的变量就是一个拥有指定名称和类型的数据存储位置. //看一个例子 package main import ( "fmt" ) func main() { var ...