这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions LINQ/docs/query-syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 `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
```

Click *Run* and the output sequence is in the reverse order.

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 query and run it yourself.
15 changes: 15 additions & 0 deletions LINQ/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Learn LINQ interactively
![dotnet try Enabled](https://img.shields.io/badge/Try_.NET-Enabled-501078.svg)

<p align ="center">
<img src ="https://user-images.githubusercontent.com/2546640/56708992-deee8780-66ec-11e9-9991-eb85abb1d10a.png" width="350">
</p>

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).

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.

## Lessons in this tutorial

- [LINQ Query Syntax](docs/query-syntax.md)
- More in upcoming PRs
14 changes: 14 additions & 0 deletions LINQ/src/LINQ.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.3.0-alpha.19405.1" />
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19405.1" />
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions LINQ/src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.Threading.Tasks;

namespace LINQ
{
///<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.
///<example>Execute: dotnet run --region "HelloWorld" to see the output</example>
///</summary>
public class Program
{
///<param name="region">Takes in the --region option from the code fence options in markdown</param>
///<param name="session">Takes in the --session option from the code fence options in markdown</param>
///<param name="package">Takes in the --package option from the code fence options in markdown</param>
///<param name="project">Takes in the --project option from the code fence options in markdown</param>
///<param name="args">Takes in any additional arguments passed in the code fence options in markdown</param>
///<see>To learn more see <a href="https://aka.ms/learntdn">our documentation</a></see>
static int Main(
string region = null,
string session = null,
string package = null,
string project = null,
string[] args = null)
{
return region switch
{
"query-syntax" => QuerySyntax(),
_ => throw new ArgumentException("A --region argument must be passed", nameof(region))
};
}

internal static int QuerySyntax()
{
#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;

foreach (var number in squaresOfOddNumbers)
Console.WriteLine(number);
#endregion
return 0;
}

}
}