📜  LINQ-查询运算符

📅  最后修改于: 2020-11-21 07:06:06             🧑  作者: Mango


形成查询模式的一组扩展方法称为LINQ标准查询运算符。作为LINQ查询表达式的构建块,这些运算符提供了一系列查询功能,例如过滤,排序,投影,聚合等。

LINQ标准查询运算符可以根据其功能分为以下几种。

  • 筛选运算符
  • 加盟运营商
  • 投影作业
  • 分类运算符
  • 分组运算符
  • 转换次数
  • 级联
  • 聚合
  • 量词操作
  • 分区操作
  • 发电业务
  • 设定作业
  • 平等
  • 元素运算符

筛选运算符

过滤是一种限制结果集的操作,以使其仅具有满足特定条件的选定元素。

显示范例

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

加盟运营商

联接是指以直接方式难以相互遵循的数据源为目标的操作。

显示范例

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 …

投影作业

投影是一种将对象转换为仅具有特定属性的全新形式的操作。

显示范例

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

分类运算符

排序操作允许根据单个或多个属性对序列的元素进行排序。

显示范例

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

分组运算符

运算符基于共同的共享属性将数据分为几组。

显示范例

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 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

转换次数

运算符更改输入对象的类型,并在各种应用中使用。

显示范例

Operator Description C# Query Expression Syntax VB Query Expression Syntax
AsEnumerable Returns the input typed as IEnumerable 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 and forces execution of a LINQ query Not Applicable Not Applicable
ToList Forces execution of a query by converting a collection to a List Not Applicable Not Applicable
ToLookup Forces execution of a query and put elements into a Lookup on basis of a key selector function Not Applicable Not Applicable

级联

执行两个序列的串联操作,就其操作而言,它与Union运算符非常相似,不同之处在于它不会删除重复项。

显示范例

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

聚合

执行任何类型的所需聚合,并允许在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()

量词操作

当序列中的某些或所有元素满足特定条件时,这些运算符返回布尔值,即True或False。

显示范例

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

分区运算符

将输入序列分为两个单独的部分,而无需重新排列序列的元素,然后返回其中一个。

显示范例

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

发电业务

代数运算符创建了一个新的值序列。

显示范例

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

设定作业

设置操作有四个运算符,每个运算符都基于不同的标准得出结果。

显示范例

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

平等

比较两个句子(可枚举)并确定它们是否完全匹配。

显示范例

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

元素运算符

除DefaultIfEmpty外,其余所有八个标准查询元素运算符返回集合中的单个元素。

显示范例

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