You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixes#45758
Update the LINQ section to highlight C# 14 extension members over the C# 3 style `this` extension methods.
In general, refer to "extension members" over "extension methods" when discussing the language feature. Use "extension methods" in the context of writing *methods* specifically, as in the query expression pattern.
Copy file name to clipboardExpand all lines: docs/csharp/linq/get-started/features-that-support-linq.md
+23-21Lines changed: 23 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,16 @@
1
1
---
2
-
title: "Language Features That Support LINQ"
2
+
title: "Language features that support LINQ"
3
3
description: Learn about C# features to use with LINQ queries and in other contexts.
4
-
ms.date: 04/22/2024
4
+
ms.date: 12/01/2025
5
+
ai-usage: ai-assisted
5
6
helpviewer_keywords:
6
7
- "LINQ [C#], features supporting LINQ"
7
8
---
8
-
# C# Features That Support LINQ
9
+
# C# features that support LINQ
9
10
10
-
## Query Expressions
11
+
## Query expressions
11
12
12
-
Query expressions use a declarative syntax similar to SQL or XQuery to query over <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> collections. At compile time, query syntax is converted to method calls to a LINQ provider's implementation of the standard query methods. Applications control the standard query operators that are in scope by specifying the appropriate namespace with a [`using`](../../language-reference/keywords/using-directive.md) directive. The following query expression takes an array of strings, groups them according to the first character in the string, and orders the groups.
13
+
Query expressions use a declarative syntax similar to SQL or XQuery to query over <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> collections. At compile time, the compiler converts query syntax to method calls to a LINQ provider's implementation of the standard query methods. Applications control the standard query operators that are in scope by specifying the appropriate namespace with a [`using`](../../language-reference/keywords/using-directive.md) directive. The following query expression takes an array of strings, groups them according to the first character in the string, and orders the groups.
13
14
14
15
```csharp
15
16
varquery=fromstrinstringArray
@@ -18,7 +19,7 @@ var query = from str in stringArray
18
19
selectstringGroup;
19
20
```
20
21
21
-
## Implicitly Typed Variables (var)
22
+
## Implicitly typed variables (var)
22
23
23
24
You can use the [var](../../language-reference/statements/declarations.md#implicitly-typed-local-variables) modifier to instruct the compiler to infer and assign the type, as shown here:
24
25
@@ -30,25 +31,25 @@ var query = from str in stringArray
30
31
selectstr;
31
32
```
32
33
33
-
Variables declared as `var` are strongly typed, just like variables whose type you specify explicitly. The use of`var` makes it possible to create anonymous types, but only for local variables. For more information, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md).
34
+
Variables declared as `var` are strongly typed, just like variables whose type you specify explicitly. Using`var` makes it possible to create anonymous types, but only for local variables. For more information, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md).
34
35
35
-
## Object and Collection Initializers
36
+
## Object and collection initializers
36
37
37
-
Object and collection initializers make it possible to initialize objects without explicitly calling a constructor for the object. Initializers are typically used in query expressions when they project the source data into a new data type. Assuming a class named `Customer` with public `Name` and `Phone` properties, the object initializer can be used as in the following code:
38
+
Object and collection initializers make it possible to initialize objects without explicitly calling a constructor for the object. You typically use initializers in query expressions when they project the source data into a new data type. Assuming a class named `Customer` with public `Name` and `Phone` properties, you can use the object initializer as in the following code:
Continuing with your `Customer` class, assume that there's a data source called `IncomingOrders`, and that for each order with a large `OrderSize`, you would like to create a new `Customer` based off of that order. A LINQ query can be executed on this data source and use object initialization to fill a collection:
44
+
Continuing with your `Customer` class, assume that there's a data source called `IncomingOrders`, and that for each order with a large `OrderSize`, you want to create a new `Customer` based off of that order. You can execute a LINQ query on this data source and use object initialization to fill a collection:
44
45
45
46
```csharp
46
47
varnewLargeOrderCustomers=fromoinIncomingOrders
47
48
whereo.OrderSize>5
48
49
selectnewCustomer { Name=o.Name, Phone=o.Phone };
49
50
```
50
51
51
-
The data source might have more properties defined than the `Customer` class such as `OrderSize`, but with object initialization, the data returned from the query is molded into the desired data type; you choose the data that is relevant to your class. As a result, you now have an <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> filled with the new `Customer`s you wanted. The preceding example can also be written in LINQ's method syntax:
52
+
The data source might have more properties defined than the `Customer` class such as `OrderSize`, but with object initialization, the data returned from the query is molded into the desired data type; you choose the data that's relevant to your class. As a result, you now have an <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> filled with the new `Customer`s you wanted. You can also write the preceding example in LINQ's method syntax:
-[Object and Collection Initializers](../../programming-guide/classes-and-structs/object-and-collection-initializers.md)
62
63
-[Query Expression Syntax for Standard Query Operators](../standard-query-operators/index.md)
63
64
64
-
## Anonymous Types
65
+
## Anonymous types
65
66
66
-
The compiler constructs an [anonymous type](../../fundamentals/types/anonymous-types.md). The type name is only available to the compiler. Anonymous types provide a convenient way to group a set of properties temporarily in a query result without having to define a separate named type. Anonymous types are initialized with a new expression and an object initializer, as shown here:
67
+
The compiler constructs an [anonymous type](../../fundamentals/types/anonymous-types.md). Only the compiler can access the type name. Anonymous types provide a convenient way to group a set of properties temporarily in a query result without having to define a separate named type. You initialize anonymous types with a new expression and an object initializer, as shown here:
67
68
68
69
```csharp
69
70
selectnew {name=cust.Name, phone=cust.Phone};
70
71
```
71
72
72
73
Beginning with C# 7, you can use [tuples](../../language-reference/builtin-types/value-tuples.md) to create unnamed types.
73
74
74
-
## Extension Methods
75
+
## Extension members
75
76
76
-
An [extension method](../../programming-guide/classes-and-structs/extension-methods.md) is a static method that can be associated with a type, so that it can be called as if it were an instance method on the type. This feature enables you to, in effect, "add" new methods to existing types without actually modifying them. The standard query operators are a set of extension methods that provide LINQ query functionality for any type that implements <xref:System.Collections.Generic.IEnumerable%601>.
77
+
An [extension member](../../programming-guide/classes-and-structs/extension-methods.md) is a static member of a static class associated with a type called the *receiver type*. You can call an extension member as if it were a member of the receiver type. This feature enables you to"add" new members to existing types without actually modifying them. The standard query operators are a set of extension methods that provide LINQ query functionality for any type that implements <xref:System.Collections.Generic.IEnumerable%601>.
77
78
78
-
## Lambda Expressions
79
+
## Lambda expressions
79
80
80
-
A [lambda expressions](../../language-reference/operators/lambda-expressions.md) is an inline function that uses the `=>` operator to separate input parameters from the function body and can be converted at compile time to a delegate or an expression tree. In LINQ programming, you encounter lambda expressions when you make direct method calls to the standard query operators.
81
+
A [lambda expression](../../language-reference/operators/lambda-expressions.md) is an inline function that uses the `=>` operator to separate input parameters from the function body and can be converted at compile time to a delegate or an expression tree. In LINQ programming, you encounter lambda expressions when you make direct method calls to the standard query operators.
81
82
82
83
## Expressions as data
83
84
84
-
Query objects are composable, meaning that you can return a query from a method. Objects that represent queries don't store the resulting collection, but rather the steps to produce the results when needed. The advantage of returning query objects from methods is that they can be further composed or modified. Therefore any return value or `out` parameter of a method that returns a query must also have that type. If a method materializes a query into a concrete <xref:System.Collections.Generic.List%601> or <xref:System.Array> type, it returns the query results instead of the query itself. A query variable that is returned from a method can still be composed or modified.
85
+
Query objects are composable, meaning that you can return a query from a method. Objects that represent queries don't store the resulting collection, but rather the steps to produce the results when needed. The advantage of returning query objects from methods is that you can further compose or modify them. Therefore, any return value or `out` parameter of a method that returns a query must also have that type. If a method materializes a query into a concrete <xref:System.Collections.Generic.List%601> or <xref:System.Array> type, it returns the query results instead of the query itself. You can still compose or modify a query variable that's returned from a method.
85
86
86
-
In the following example, the first method `QueryMethod1` returns a query as a return value, and the second method `QueryMethod2` returns a query as an `out` parameter (`returnQ` in the example). In both cases, it's a query that is returned, not query results.
87
+
In the following example, the first method `QueryMethod1` returns a query as a return value, and the second method `QueryMethod2` returns a query as an `out` parameter (`returnQ` in the example). In both cases, it's a query that's returned, not query results.
You can modify a query by using query composition. In this case, the previous query object is used to create a new query object. This new object returns different results than the original query object.
0 commit comments