Location>code7788 >text

What's New in LINQ in .NET 9

Popularity:527 ℃/2024-09-10 08:02:07

Introduction to LINQ

Language-Integrated Querying (LINQ) is a collective term for a set of techniques for integrating querying functionality directly into the C# language. Data queries have traditionally been represented as simple strings with no compile-time type checking or IntelliSense support. In addition, a different query language needs to be understood for each type of data source: SQL databases, XML documents, various Web services, etc. With LINQ, queries become top-level language constructs, like classes, methods, and events.

Installing .NET 9

A prerequisite for experiencing the additions to LINQ is the installation of the .NET 9 environment, the successor to .NET 8, with a particular focus on cloud-native applications and performance. It will be supported for 18 months as a Standard Term Support (STS) release.

  • Download .NET 9.0:/zh-cn/download/dotnet/9.0

Verify that the installation was successful

dotnet --list-sdks

NET 9 SDK not currently available in Visual Studio 2022

Note: The .NET 9 SDK is not currently available in Visual Studio 2022, so it is not possible to select .NET 9 as the target framework in Visual Studio 2022.

Writing Core Web Applications in VS Code

  • /s/kKVPdxp9p-7QZ45XMosDWw

Creating .NET 9 Console Applications in VS Code

Open the terminal from VS Code

Run the following command:

dotnet new console --framework net9.0 --use-program-main

Running the application

dotnet run

CountBy method

This method, CountBy, allows the developer to press keys to aggregate elements in a collection and count the number of occurrences of each key. This makes it very simple to calculate the frequency of a particular element in a dataset.

    public static void CountByExample()
    {
// here wordCounts is a dictionary containing key-value pairs for each word and its occurrences
            var sourceText = "This is a test text. This is only a test. This is the best. This,This,This";
            KeyValuePair<string, int> mostFrequentWord = sourceText
            .Split(new char[] { ' ', '.', ',' }, )
            .Select(word => ())
            .CountBy(word => word)
            .MaxBy(pair => );

($"The most common words are: '{}' {}");
   }

Output results:

AggregateBy Method

AggregateBy This method provides more powerful aggregation capabilities. The developer can define an aggregation logic (e.g. summing, averaging, etc.) and press keys for aggregation. This method is useful when complex calculations need to be performed on elements in a collection based on keys.

    public static void AggregateByExample()
    {
        (string id, int score)[] data =
            [
                ("0", 88),
                ("1", 5),
                ("2", 4),
                ("1", 10),
                ("6", 5),
                ("4", 10),
                ("6", 25)
            ];

// aggregatedData is a sequence containing elements that are grouped by name and a total score is calculated
        var aggregatedData =
            (
                keySelector: entry => ,
                seed: 0,
                (totalScore, curr) => totalScore + 
                );

        foreach (var item in aggregatedData)
        {
            (item);
        }
    }

Output results:

Index<TSource>(IEnumerable<TSource>) methodologies

leverageIndex<TSource>(IEnumerable<TSource>), you can quickly extract implicit indexes for enumerable items. Code (such as the following code snippet) can now be written to automatically index items in a collection.

    public static void IndexExample()
    {
        var lines = new List<string> { "First line", "Second line", "Third line" };
        foreach (var (index, line) in ())
        {
            ($"Line {index + 1}: {line}");
        }
    }

Output results:

reference article

  • /zh-cn/dotnet/core/whats-new/dotnet-9/overview#linq

C#/.NET/.NET Core Picking Up the Pieces

  • /YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/