From 0aec14c074464e4d5609b6f24d631766647a95f0 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 21 Aug 2019 18:09:39 -0400 Subject: [PATCH 1/4] testing a framework Initial checkin --- LINQ/readme.md | 20 ++++++++++++++++++ LINQ/src/LINQ.csproj | 14 +++++++++++++ LINQ/src/Program.cs | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 LINQ/readme.md create mode 100644 LINQ/src/LINQ.csproj create mode 100644 LINQ/src/Program.cs diff --git a/LINQ/readme.md b/LINQ/readme.md new file mode 100644 index 0000000..46a3c8d --- /dev/null +++ b/LINQ/readme.md @@ -0,0 +1,20 @@ +# Creating interactive documentation with Try .NET + +## Setup +If you haven't installed the `dotnet try` global tool, you can install it by following the instructions [here](https://github.com/dotnet/try#setup). + +## This code fence will run the HelloWorld method from Program.cs + +```cs --source-file ./LINQ-examples/Program.cs --project ./LINQ-examples/LINQ-examples.csproj --region HelloWorld +``` + +## Learn More +To learn more about the `dotnet try` features, at the command line execute + +```console +dotnet try demo +``` + +## Feedback + +We love to hear from you. If you have any suggestions or feedback please reach out to us on [GitHub](https://github.com/dotnet/try) diff --git a/LINQ/src/LINQ.csproj b/LINQ/src/LINQ.csproj new file mode 100644 index 0000000..a15c94d --- /dev/null +++ b/LINQ/src/LINQ.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.0 + 8.0 + + + + + + + + diff --git a/LINQ/src/Program.cs b/LINQ/src/Program.cs new file mode 100644 index 0000000..1f9b0e5 --- /dev/null +++ b/LINQ/src/Program.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; + +namespace LINQ +{ + ///This program uses System.CommandLine.DragonFruit to accept command line arguments from command line. + ///Execute: dotnet run --region "HelloWorld" to see the output + /// + public class Program + { + ///Takes in the --region option from the code fence options in markdown + ///Takes in the --session option from the code fence options in markdown + ///Takes in the --package option from the code fence options in markdown + ///Takes in the --project option from the code fence options in markdown + ///Takes in any additional arguments passed in the code fence options in markdown + ///To learn more see our documentation + static int Main( + string region = null, + string session = null, + string package = null, + string project = null, + string[] args = null) + { + return region switch + { + "HelloWorld" => HelloWorld(), + "DateTime" => DateTime(), + _ => throw new ArgumentException("A --region argument must be passed", nameof(region)) + }; + } + + internal static int HelloWorld() + { + #region HelloWorld + Console.WriteLine("Hello World!"); + #endregion + return 0; + } + + internal static int DateTime() + { + #region DateTime + Console.WriteLine(System.DateTime.Now); + #endregion + return 0; + } + } +} \ No newline at end of file From 02e623e32d6a31c62e483d22a887dd3ad9e10e5a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 21 Aug 2019 22:07:46 -0400 Subject: [PATCH 2/4] initial draft of the first lesson. --- LINQ/docs/query-syntax.md | 34 ++++++++++++++++++++++++++++++++++ LINQ/readme.md | 25 ++++++++++--------------- LINQ/src/LINQ.csproj | 4 ++-- LINQ/src/Program.cs | 23 +++++++++++------------ 4 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 LINQ/docs/query-syntax.md diff --git a/LINQ/docs/query-syntax.md b/LINQ/docs/query-syntax.md new file mode 100644 index 0000000..28aa659 --- /dev/null +++ b/LINQ/docs/query-syntax.md @@ -0,0 +1,34 @@ +# LINQ Query Syntax + +Click the `run` icon to get started. Try the following LINQ query: + +``` cs --region query-syntax --source-file ../src/Program.cs --project ../src/LINQ.csproj +``` + +The preceding query shows the major sections of a query: a source for the query, a series of transformations, and a projection that produces results. Following the query, the `foreach` loop consumes the data elements produced by the query. Let's look at each of these components in turn. + +First, you have the data source: + +```csharp +from n in sequence +``` + +The `from` clause defines the the *range variable*, `n`, and specifies the the *source*, `sequence`. The *source* specifies the producer of all the elements processed in the query. The *range variable* defines a variable that holds each element of the *source* in turn as the query executes. + +Next, you write any number of *transformations*: + +```csharp +where n % 2 == 1 +``` + +Every *transformation* produces an output sequence from its input sequence. This transformation is a *filter*. The elements from the input sequence that satisfy the condition `n % 2 == 1` are produced as the output sequence. As you work through more lessons in this tutorial, you'll create more complicated transformations. + +Finally, every query ends with a *projection*: + +```csharp +select n * n; +``` + +Every query ends with a *projection* that produces either a new sequence, or a single value. This first example produces a sequence where every element is the square of the input sequence. + +You can modify any of these sections in the uery and run it yourself. diff --git a/LINQ/readme.md b/LINQ/readme.md index 46a3c8d..e22b799 100644 --- a/LINQ/readme.md +++ b/LINQ/readme.md @@ -1,20 +1,15 @@ -# Creating interactive documentation with Try .NET +# Learn LINQ interactively +![dotnet try Enabled](https://img.shields.io/badge/Try_.NET-Enabled-501078.svg) -## Setup -If you haven't installed the `dotnet try` global tool, you can install it by following the instructions [here](https://github.com/dotnet/try#setup). +

+ +

-## This code fence will run the HelloWorld method from Program.cs +Language-Integrated Query (LINQ) is a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. With LINQ, a query is a first-class language construct, just like classes, methods, events. You write queries against strongly typed collections of objects by using language keywords and familiar operators. The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (Entity Framework), and XML (LINQ to XML). -```cs --source-file ./LINQ-examples/Program.cs --project ./LINQ-examples/LINQ-examples.csproj --region HelloWorld -``` +You can explore LINQ in this interactive tutorial. Each lesson teaches a new LINQ concept. You start with the fundamental syntax. From there, each additional lesson adds new concepts. If you already have some experience with LINQ, you can skip to specific sections that interest you. -## Learn More -To learn more about the `dotnet try` features, at the command line execute +## Lessons in this tutorial -```console -dotnet try demo -``` - -## Feedback - -We love to hear from you. If you have any suggestions or feedback please reach out to us on [GitHub](https://github.com/dotnet/try) +- [LINQ Query Syntax](docs/query-syntax.md) +- More in upcoming PRs diff --git a/LINQ/src/LINQ.csproj b/LINQ/src/LINQ.csproj index a15c94d..9a9fc0e 100644 --- a/LINQ/src/LINQ.csproj +++ b/LINQ/src/LINQ.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/LINQ/src/Program.cs b/LINQ/src/Program.cs index 1f9b0e5..3061651 100644 --- a/LINQ/src/Program.cs +++ b/LINQ/src/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading.Tasks; namespace LINQ @@ -23,26 +24,24 @@ static int Main( { return region switch { - "HelloWorld" => HelloWorld(), - "DateTime" => DateTime(), + "query-syntax" => QuerySyntax(), _ => throw new ArgumentException("A --region argument must be passed", nameof(region)) }; } - internal static int HelloWorld() + internal static int QuerySyntax() { - #region HelloWorld - Console.WriteLine("Hello World!"); - #endregion - return 0; - } + #region query-syntax + var sequence = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + var squaresOfOddNumbers = from n in sequence + where n % 2 == 1 + select n * n; - internal static int DateTime() - { - #region DateTime - Console.WriteLine(System.DateTime.Now); + foreach (var number in squaresOfOddNumbers) + Console.WriteLine(number); #endregion return 0; } + } } \ No newline at end of file From c22e30850b864667d44d423421eaa777b27f6391 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 22 Aug 2019 11:44:42 -0400 Subject: [PATCH 3/4] Review and update. --- LINQ/docs/query-syntax.md | 10 ++++++++-- LINQ/src/LINQ.csproj | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/LINQ/docs/query-syntax.md b/LINQ/docs/query-syntax.md index 28aa659..2b38a11 100644 --- a/LINQ/docs/query-syntax.md +++ b/LINQ/docs/query-syntax.md @@ -21,7 +21,13 @@ Next, you write any number of *transformations*: where n % 2 == 1 ``` -Every *transformation* produces an output sequence from its input sequence. This transformation is a *filter*. The elements from the input sequence that satisfy the condition `n % 2 == 1` are produced as the output sequence. As you work through more lessons in this tutorial, you'll create more complicated transformations. +Every *transformation* produces an output sequence from its input sequence. This transformation is a *filter*. The elements from the input sequence that satisfy the condition `n % 2 == 1` are produced as the output sequence. As you work through more lessons in this tutorial, you'll create more complicated transformations. Another common transformation is to *order* the results. Add the following line between the `where` clause and the `select` clause: + +```csharp +orderby n descending +``` + +Click *Run* and the output sequence is in the reverse order. Finally, every query ends with a *projection*: @@ -31,4 +37,4 @@ select n * n; Every query ends with a *projection* that produces either a new sequence, or a single value. This first example produces a sequence where every element is the square of the input sequence. -You can modify any of these sections in the uery and run it yourself. +You can modify any of these sections in the query and run it yourself. diff --git a/LINQ/src/LINQ.csproj b/LINQ/src/LINQ.csproj index 9a9fc0e..1ed192f 100644 --- a/LINQ/src/LINQ.csproj +++ b/LINQ/src/LINQ.csproj @@ -7,8 +7,8 @@ - - + + From 53b2b1e32dc4a172decd6db3a9f0a4d304e4285f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 23 Aug 2019 12:04:50 -0400 Subject: [PATCH 4/4] One final edit. This PR should briefly define lambda expressions. --- LINQ/docs/query-syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LINQ/docs/query-syntax.md b/LINQ/docs/query-syntax.md index 2b38a11..1cd5bd7 100644 --- a/LINQ/docs/query-syntax.md +++ b/LINQ/docs/query-syntax.md @@ -21,7 +21,7 @@ Next, you write any number of *transformations*: where n % 2 == 1 ``` -Every *transformation* produces an output sequence from its input sequence. This transformation is a *filter*. The elements from the input sequence that satisfy the condition `n % 2 == 1` are produced as the output sequence. As you work through more lessons in this tutorial, you'll create more complicated transformations. Another common transformation is to *order* the results. Add the following line between the `where` clause and the `select` clause: +Every *transformation* produces an output sequence from its input sequence. This transformation is a *filter*. The `where` keyword defines a filter. The code `n % 2 == 1` is a *lambda expression* that defines the condition for elements that pass the filter. As you work through more lessons in this tutorial, you'll create more complicated transformations. These transformations use *lambda expressions* that can be more complicated expressions to define the behavior of the query. Another common transformation is to *order* the results. Add the following line between the `where` clause and the `select` clause: ```csharp orderby n descending