C# data processing LINQ language integrated query

(Language Intergated Query) is also the language integrated query

Why use LINQ?

In engineering, we must define classes or structures to store data. These data will be temporarily stored in memory. Now when we want to complete some common tasks such as searching, filtering, etc., how do we go about it? do it?
  We can write our own code to traverse each object in the collection and check each field of the variable to see if it meets the conditions. Stories like this have happened so many times, how could Microsoft tolerate such a mentally retarded thing happening in C#? Therefore, the designers of C# decided to integrate the query syntax in C# to minimize the need for programmers to write similar code.
  This is what we call LINQ (Language Intergated Query), which is a language integrated query. We can use the same syntax to access different data sources.

Almost all applications need to process data, and most programs complete these operations through custom logic. The disadvantage of this is that the code logic will be tightly coupled with the structure of the data it processes, and if the data structure changes, it may bring a lot of code changes.
  In order to solve this problem, C# abstracts these data processing codes and provides them to the majority of developers. This is LINQ.
  The syntax of LINQ is similar to relational and hierarchical query languages ​​(SQL and XQuery), we can make changes to the data structure without changing the query code. LINQ is more flexible than SQL and can handle a wider range of logical data structures. Of course, these data structures need to implement the IEnumerable or IEnumerable<T> interface before LINQ query can be performed.

Expression Basic Syntax

A LINQ query expression begins with a from clause and ends with a select or group clause. There may be zero or more from, let, where, join, or orderby clauses between these two clauses.

In C# 3.0, some new keywords were introduced for Linq, they are:

from join where group into let orderby select

Students who are familiar with Sql look familiar. In fact, their meaning in Linq is similar to that in SQL, so it will be easy to understand.

  Each from clause is a generator that will introduce a range variable that includes the elements of the Sequence. Each let clause introduces a scope variable to represent the value computed from the previous scope variable. Each where clause is a filter to exclude items from the results. Each join clause compares the specified source sequence key with the keys of other sequences to produce matching pairs. Each orderby clause reorders the items according to the specified criteria. The final select or group clause specifies the representation of the result according to the scope variable. Finally, you can use the into clause to concatenate queries, treating a query result as a generator for subsequent queries.

Standard query operators

 Before understanding LINQ query expressions, how can we not understand its query operators?

 

How to use the let clause

There are two main uses of the let clause:

1) Create an enumerable type that can be used to query itself;

2) Store a queried temporary variable for subsequent operations.

The let clause is also relatively simple, and we recognize it through an example.

We want to find all words starting with vowels (a, e, i, o, u) from a set of sentences:

string [] sentences = new string[]{"It is a beautiful day today.",
                                                "Coding will change your life.",
                                                "The early bird chatches the worm."};
 
            var vowelWords = from sentence in sentences
                             let words = sentence.Split(' ')
                             from word in words
                             let lowerWord = word.ToLower()
                             where lowerWord[0] == 'a' ||
                                   lowerWord[0] == 'e' ||
                                   lowerWord[0] == 'i' ||
                                   lowerWord[0] == 'o' ||
                                   lowerWord[0] == 'u'
                             select word;
            foreach (var vowelWord in vowelWords)
            {
                    Console.WriteLine(vowelWord);
            }

In this query, we used two let clauses, corresponding to the two usages above.

The first let clause: separate the sentence into words based on spaces, and use the separated words as the data source for the next query;

The second let clause: Convert each word to lowercase to facilitate subsequent operations.

 

The Let of LinQ Query Expressions Commonly Used in C# Department
 Old column - CSDN blog_linq expression

Guess you like

Origin blog.csdn.net/qq_42672770/article/details/123413847