Skip to content

Commit adc26c9

Browse files
jmarkmanBillWagner
authored andcommitted
JMarkman LINQ article improvements (dotnet#8475)
* updated linq docs page - added data projection via object initializer example - added method syntax version of linq query - code snippet in Anonymous Types section was missing a csharp tag - removed Auto-Implemented Properties section * increased specificity and added ref to query page - modified introductory line to specify what example is being referenced - added a "hammer the point home" line after the query syntax LINQ projection empowers us b/c we choose data that is relevant to our class - added link to Query Expression Syntax for Standard Query Operators for query syntax to method syntax translation
1 parent 8ffbef1 commit adc26c9

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

docs/csharp/programming-guide/concepts/linq/features-that-support-linq.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,26 @@ var query = from str in stringArray
4141
```csharp
4242
Customer cust = new Customer { Name = "Mike", Phone = "555-1212" };
4343
```
44-
45-
For more information, see [Object and Collection Initializers](../../../../csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md).
46-
44+
Continuing with our `Customer` class, assume that there is a data source called `IncomingOrders`, and that for each order with a large `OrderSize`, we 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:
45+
```csharp
46+
var newLargeOrderCustomers = from o in IncomingOrders
47+
where o.OrderSize > 5
48+
select new Customer { Name = o.Name, Phone = o.Phone };
49+
```
50+
The data source may have more properties lying under the hood than the `Customer` class such as `OrderSize`, but with object initialization, the data returned from the query is molded into the desired data type; we choose the data that is relevant to our class. As a result, we now have an `IEnumerable` filled with the new `Customer`s we wanted. The above can also be written in LINQ's method syntax:
51+
```csharp
52+
var newLargeOrderCustomers = IncomingOrders.Where(x => x.OrderSize > 5).Select(y => new Customer { Name = y.Name, Phone = y.Phone });
53+
```
54+
For more information, see:
55+
56+
- [Object and Collection Initializers](../../../../csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md)
57+
58+
- [Query Expression Syntax for Standard Query Operators](../../../../csharp/programming-guide/concepts/linq/query-expression-syntax-for-standard-query-operators.md)
59+
4760
## Anonymous Types
4861
An anonymous type is constructed by the compiler and 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:
4962

50-
```
63+
```csharp
5164
select new {name = cust.Name, phone = cust.Phone};
5265
```
5366

@@ -68,16 +81,7 @@ select new {name = cust.Name, phone = cust.Phone};
6881
- [Lambda Expressions](../../../../csharp/programming-guide/statements-expressions-operators/lambda-expressions.md)
6982

7083
- [Expression Trees (C#)](../../../../csharp/programming-guide/concepts/expression-trees/index.md)
71-
72-
## Auto-Implemented Properties
73-
Auto-implemented properties make property-declaration more concise. When you declare a property as shown in the following example, the compiler will create a private, anonymous backing field that is not accessible except through the property getter and setter.
74-
75-
```csharp
76-
public string Name {get; set;}
77-
```
78-
79-
For more information, see [Auto-Implemented Properties](../../../../csharp/programming-guide/classes-and-structs/auto-implemented-properties.md).
80-
84+
8185
## See Also
8286

8387
- [Language-Integrated Query (LINQ) (C#)](../../../../csharp/programming-guide/concepts/linq/index.md)

0 commit comments

Comments
 (0)