Linq 简明教程
LINQ - Query Operators
形成查询模式的一组扩展方法被称为 LINQ 标准查询运算符。作为 LINQ 查询表达式的构建块,这些运算符提供了一系列查询功能,如筛选、排序、投影、聚合等。
A set of extension methods forming a query pattern is known as LINQ Standard Query Operators. As building blocks of LINQ query expressions, these operators offer a range of query capabilities like filtering, sorting, projection, aggregation, etc.
LINQ 标准查询运算符可以根据其功能归类为以下类。
LINQ standard query operators can be categorized into the following ones on the basis of their functionality.
-
Filtering Operators
-
Join Operators
-
Projection Operations
-
Sorting Operators
-
Grouping Operators
-
Conversions
-
Concatenation
-
Aggregation
-
Quantifier Operations
-
Partition Operations
-
Generation Operations
-
Set Operations
-
Equality
-
Element Operators
Filtering Operators
筛选是一种操作,用于限制结果集,以便它只有满足特定条件的选定元素。
Filtering is an operation to restrict the result set such that it has only selected elements satisfying a particular condition.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
where |
Filter values based on a predicate function |
where |
Where |
OfType |
Filter values based on their ability to be as a specified type |
Not Applicable |
Not Applicable |
Join Operators
联接是指以直接方式针对难以相互跟踪关系的数据源的操作。
Joining refers to an operation in which data sources with difficult to follow relationships with each other in a direct way are targeted.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
Join |
The operator join two sequences on basis of matching keys |
join … in … on … equals … |
From x In …, y In … Where x.a = y.a |
GroupJoin |
Join two sequences and group the matching elements |
join … in … on … equals … into … |
Group Join … In … On … |
Projection Operations
投影是一种将对象转换成具有特定属性的全新形式的操作。
Projection is an operation in which an object is transformed into an altogether new form with only specific properties.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
Select |
The operator projects values on basis of a transform function |
select |
Select |
SelectMany |
The operator project the sequences of values which are based on a transform function as well as flattens them into a single sequence |
Use multiple from clauses |
Use multiple From clauses |
Sorting Operators
排序操作允许基于单个或多个属性对序列的元素进行排序。
A sorting operation allows ordering the elements of a sequence on basis of a single or more attributes.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
OrderBy |
The operator sort values in an ascending order |
orderby |
Order By |
OrderByDescending |
The operator sort values in a descending order |
orderby … descending |
Order By … Descending |
ThenBy |
Executes a secondary sorting in an ascending order |
orderby …, … |
Order By …, … |
ThenByDescending |
Executes a secondary sorting in a descending order |
orderby …, … descending |
Order By …, … Descending |
Reverse |
Performs a reversal of the order of the elements in a collection |
Not Applicable |
Not Applicable |
Grouping Operators
运算符基于常用共享属性将数据放入到某些组中。
The operators put data into some groups based on a common shared attribute.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
GroupBy |
Organize a sequence of items in groups and return them as an IEnumerable collection of type IGrouping<key, element> |
group … by -or- group … by … into … |
Group … By … Into … |
ToLookup |
Execute a grouping operation in which a sequence of key pairs are returned |
Not Applicable |
Not Applicable |
Conversions
运算符会更改输入对象的类型,应用于各类应用程序中。
The operators change the type of input objects and are used in a diverse range of applications.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
AsEnumerable |
Returns the input typed as IEnumerable<T> |
Not Applicable |
Not Applicable |
AsQueryable |
A (generic) IEnumerable is converted to a (generic) IQueryable |
Not Applicable |
Not Applicable |
Cast |
Performs casting of elements of a collection to a specified type |
Use an explicitly typed range variable. Eg:from string str in words |
From … As … |
OfType |
Filters values on basis of their , depending on their capability to be cast to a particular type |
Not Applicable |
Not Applicable |
ToArray |
Forces query execution and does conversion of a collection to an array |
Not Applicable |
Not Applicable |
ToDictionary |
On basis of a key selector function set elements into a Dictionary<TKey, TValue> and forces execution of a LINQ query |
Not Applicable |
Not Applicable |
ToList |
Forces execution of a query by converting a collection to a List<T> |
Not Applicable |
Not Applicable |
ToLookup |
Forces execution of a query and put elements into a Lookup<TKey, TElement> on basis of a key selector function |
Not Applicable |
Not Applicable |
Concatenation
执行两个序列的连接,并且除不删除重复项这一事实外,在操作方面与 Union 运算符很相似。
Performs concatenation of two sequences and is quite similar to the Union operator in terms of its operation except of the fact that this does not remove duplicates.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
Concat |
Two sequences are concatenated for the formation of a single one sequence. |
Not Applicable |
Not Applicable |
Aggregation
执行任何类型的所需聚合,并允许在 LINQ 中创建自定义聚合。
Performs any type of desired aggregation and allows creating custom aggregations in LINQ.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
Aggregate |
Operates on the values of a collection to perform custom aggregation operation |
Not Applicable |
Not Applicable |
Average |
Average value of a collection of values is calculated |
Not Applicable |
Aggregate … In … Into Average() |
Count |
Counts the elements satisfying a predicate function within collection |
Not Applicable |
Aggregate … In … Into Count() |
LonCount |
Counts the elements satisfying a predicate function within a huge collection |
Not Applicable |
Aggregate … In … Into LongCount() |
Max |
Find out the maximum value within a collection |
Not Applicable |
Aggregate … In … Into Max() |
Min |
Find out the minimum value existing within a collection |
Not Applicable |
Aggregate … In … Into Min() |
Sum |
Find out the sum of a values within a collection |
Not Applicable |
Aggregate … In … Into Sum() |
Quantifier Operations
当序列中某些或所有元素满足特定条件时,这些运算符会返回布尔值,即 True 或 False。
These operators return a Boolean value i.e. True or False when some or all elements within a sequence satisfy a specific condition.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
All |
Returns a value ‘True’ if all elements of a sequence satisfy a predicate condition |
Not Applicable |
Aggregate … In … Into All(…) |
Any |
Determines by searching a sequence that whether any element of the same satisfy a specified condition |
Not Applicable |
Aggregate … In … Into Any() |
Contains |
Returns a ‘True’ value if finds that a specific element is there in a sequence if the sequence doe not contains that specific element , ‘false’ value is returned |
Not Applicable |
Not Applicable |
Partition Operators
将输入序列划分为不重新排列序列元素的两个独立部分,然后返回其中之一。
Divide an input sequence into two separate sections without rearranging the elements of the sequence and then returning one of them.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
Skip |
Skips some specified number of elements within a sequence and returns the remaining ones |
Not Applicable |
Skip |
SkipWhile |
Same as that of Skip with the only exception that number of elements to skip are specified by a Boolean condition |
Not Applicable |
Skip While |
Take |
Take a specified number of elements from a sequence and skip the remaining ones |
Not Applicable |
Take |
TakeWhile |
Same as that of Take except the fact that number of elements to take are specified by a Boolean condition |
Not Applicable |
Take While |
Generation Operations
新值序列由生成运算符创建。
A new sequence of values is created by generational operators.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
DefaultIfEmpty |
When applied to an empty sequence, generate a default element within a sequence |
Not Applicable |
Not Applicable |
Empty |
Returns an empty sequence of values and is the most simplest generational operator |
Not Applicable |
Not Applicable |
Range |
Generates a collection having a sequence of integers or numbers |
Not Applicable |
Not Applicable |
Repeat |
Generates a sequence containing repeated values of a specific length |
Not Applicable |
Not Applicable |
Set Operations
集合运算有四个运算符,每个运算符根据不同的条件产生结果。
There are four operators for the set operations, each yielding a result based on different criteria.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
Distinct |
Results a list of unique values from a collection by filtering duplicate data if any |
Not Applicable |
Distinct |
Except |
Compares the values of two collections and return the ones from one collection who are not in the other collection |
Not Applicable |
Not Applicable |
Intersect |
Returns the set of values found t be identical in two separate collections |
Not Applicable |
Not Applicable |
Union |
Combines content of two different collections into a single list that too without any duplicate content |
Not Applicable |
Not Applicable |
Equality
比较两个句子(可枚举的),并确定它们是否完全匹配。
Compares two sentences (enumerable ) and determine are they an exact match or not.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
SequenceEqual |
Results a Boolean value if two sequences are found to be identical to each other |
Not Applicable |
Not Applicable |
Element Operators
除了 DefaultIfEmpty,所有其他八个标准查询元素运算符都会从一个集合返回一个单一元素。
Except the DefaultIfEmpty, all the rest eight standard query element operators return a single element from a collection.
Operator |
Description |
C# Query Expression Syntax |
VB Query Expression Syntax |
ElementAt |
Returns an element present within a specific index in a collection |
Not Applicable |
Not Applicable |
ElementAtOrDefault |
Same as ElementAt except of the fact that it also returns a default value in case the specific index is out of range |
Not Applicable |
Not Applicable |
First |
Retrieves the first element within a collection or the first element satisfying a specific condition |
Not Applicable |
Not Applicable |
FirstOrDefault |
Same as First except the fact that it also returns a default value in case there is no existence of such elements |
Not Applicable |
Not Applicable |
Last |
Retrieves the last element present in a collection or the last element satisfying a specific condition |
Not Applicable |
Not Applicable |
LastOrDefault |
Same as Last except the fact that it also returns a default value in case there is no existence of any such element |
Not Applicable |
Not Applicable |
Single |
Returns the lone element of a collection or the lone element that satisfy a certain condition |
Not Applicable |
Not Applicable |
SingleOrDefault |
Same as Single except that it also returns a default value if there is no existence of any such lone element |
Not Applicable |
Not Applicable |
DefaultIfEmpty |
Returns a default value if the collection or list is empty or null |
Not Applicable |
Not Applicable |