Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 1db2665

Browse files
authored
Merge pull request #39 from BillWagner/LINQ-basic-query
LINQ basic query
2 parents cbce8f1 + 53b2b1e commit 1db2665

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

LINQ/docs/query-syntax.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# LINQ Query Syntax
2+
3+
Click the `run` icon to get started. Try the following LINQ query:
4+
5+
``` cs --region query-syntax --source-file ../src/Program.cs --project ../src/LINQ.csproj
6+
```
7+
8+
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.
9+
10+
First, you have the data source:
11+
12+
```csharp
13+
from n in sequence
14+
```
15+
16+
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.
17+
18+
Next, you write any number of *transformations*:
19+
20+
```csharp
21+
where n % 2 == 1
22+
```
23+
24+
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:
25+
26+
```csharp
27+
orderby n descending
28+
```
29+
30+
Click *Run* and the output sequence is in the reverse order.
31+
32+
Finally, every query ends with a *projection*:
33+
34+
```csharp
35+
select n * n;
36+
```
37+
38+
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.
39+
40+
You can modify any of these sections in the query and run it yourself.

LINQ/readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Learn LINQ interactively
2+
![dotnet try Enabled](https://img.shields.io/badge/Try_.NET-Enabled-501078.svg)
3+
4+
<p align ="center">
5+
<img src ="https://user-images.githubusercontent.com/2546640/56708992-deee8780-66ec-11e9-9991-eb85abb1d10a.png" width="350">
6+
</p>
7+
8+
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).
9+
10+
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.
11+
12+
## Lessons in this tutorial
13+
14+
- [LINQ Query Syntax](docs/query-syntax.md)
15+
- More in upcoming PRs

LINQ/src/LINQ.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
<LangVersion>8.0</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.3.0-alpha.19405.1" />
11+
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19405.1" />
12+
</ItemGroup>
13+
14+
</Project>

LINQ/src/Program.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
5+
namespace LINQ
6+
{
7+
///<summary>This program uses <a href="https://github.com/dotnet/command-line-api/wiki/DragonFruit-overview">System.CommandLine.DragonFruit</a> to accept command line arguments from command line.
8+
///<example>Execute: dotnet run --region "HelloWorld" to see the output</example>
9+
///</summary>
10+
public class Program
11+
{
12+
///<param name="region">Takes in the --region option from the code fence options in markdown</param>
13+
///<param name="session">Takes in the --session option from the code fence options in markdown</param>
14+
///<param name="package">Takes in the --package option from the code fence options in markdown</param>
15+
///<param name="project">Takes in the --project option from the code fence options in markdown</param>
16+
///<param name="args">Takes in any additional arguments passed in the code fence options in markdown</param>
17+
///<see>To learn more see <a href="https://aka.ms/learntdn">our documentation</a></see>
18+
static int Main(
19+
string region = null,
20+
string session = null,
21+
string package = null,
22+
string project = null,
23+
string[] args = null)
24+
{
25+
return region switch
26+
{
27+
"query-syntax" => QuerySyntax(),
28+
_ => throw new ArgumentException("A --region argument must be passed", nameof(region))
29+
};
30+
}
31+
32+
internal static int QuerySyntax()
33+
{
34+
#region query-syntax
35+
var sequence = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
36+
var squaresOfOddNumbers = from n in sequence
37+
where n % 2 == 1
38+
select n * n;
39+
40+
foreach (var number in squaresOfOddNumbers)
41+
Console.WriteLine(number);
42+
#endregion
43+
return 0;
44+
}
45+
46+
}
47+
}

0 commit comments

Comments
 (0)