Skip to content

Commit 5580736

Browse files
authored
Clarify usage of string interpolation in LINQ Data Transformation (#18764)
* clarify 'performing operations on source elements' - refactor query description to clearly describe steps in operation - add brief explanation of string interpolation and link to string interpolation page * update code snippet - change output sequence variable name from 'query' to 'output' - add linq example using method syntax in addition to query syntax - add allman braces to foreach loop printing results * add extra sentence to include concatenation
1 parent de5af20 commit 5580736

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

docs/csharp/programming-guide/concepts/linq/data-transformations-with-linq.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ Language-Integrated Query (LINQ) is not only about retrieving data. It is also a
8484
For more information, see [Creating XML Trees in C# (LINQ to XML)](./creating-xml-trees-linq-to-xml-2.md).
8585

8686
## Performing Operations on Source Elements
87-
An output sequence might not contain any elements or element properties from the source sequence. The output might instead be a sequence of values that is computed by using the source elements as input arguments. The following simple query, when it is executed, outputs a sequence of strings whose values represent a calculation based on the source sequence of elements of type `double`.
87+
An output sequence might not contain any elements or element properties from the source sequence. The output might instead be a sequence of values that is computed by using the source elements as input arguments.
88+
89+
The following query will take a sequence of numbers that represent radii of circles, calculate the area for each radius, and return an output sequence containing strings formatted with the calculated area.
90+
91+
Each string for the output sequence will be formatted using [string interpolation](../../../language-reference/tokens/interpolated.md). An interpolated string will have a `$` in front of the string's opening quotation mark, and operations can be performed within curly braces placed inside the interpolated string. Once those operations are performed, the results will be concatenated.
8892

8993
> [!NOTE]
9094
> Calling methods in query expressions is not supported if the query will be translated into some other domain. For example, you cannot call an ordinary C# method in [!INCLUDE[vbtecdlinq](~/includes/vbtecdlinq-md.md)] because SQL Server has no context for it. However, you can map stored procedures to methods and call those. For more information, see [Stored Procedures](../../../../framework/data/adonet/sql/linq/stored-procedures.md).

samples/snippets/csharp/VS_Snippets_VBCSharp/CsLINQGettingStarted/CS/Class1.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,22 @@ static void Main()
252252
// Data source.
253253
double[] radii = { 1, 2, 3 };
254254

255-
// Query.
256-
IEnumerable<string> query =
255+
// LINQ query using method syntax.
256+
IEnumerable<string> output =
257+
radii.Select(r => $"Area for a circle with a radius of '{r}' = {r * r * Math.PI:F2}");
258+
259+
/*
260+
// LINQ query using query syntax.
261+
IEnumerable<string> output =
257262
from rad in radii
258263
select $"Area = {rad * rad * Math.PI:F2}";
264+
*/
259265

260-
// Query execution.
261-
foreach (string s in query)
266+
foreach (string s in output)
267+
{
262268
Console.WriteLine(s);
263-
269+
}
270+
264271
// Keep the console open in debug mode.
265272
Console.WriteLine("Press any key to exit.");
266273
Console.ReadKey();

0 commit comments

Comments
 (0)