SoFunction
Updated on 2025-03-01

Detailed explanation of the introduction to LINQ (Previous article)

Preface

Recently, I was having a technical interview with our boss (I was an attendee) and found that few of them came to the interview and even had a little understanding of LINQ. This made me wonder. Is it really not time to understand or learn the basics of this application since the release of LINQ with 2008? Even when asked what LINQ is, the answerer didn’t think about LINQ TO SQL, crashed! That's right, LINQ can do nothing except SQL? No. Therefore, I will share with you the learning LINQ. This article is suitable for the following readers. If you are not in line, please give me a favor. 3Q

  • Never touched LINQ
  • I have known about LINQ but have never tried it
  • Planning to learn LINQ

Introduction

What is LINQ? To quote the official term "LINQ" is an innovative feature introduced in Visual Studio 2008 and .NET work version 3.5, which builds a bridge between the object and data fields." So what LINQ brings us, please see the following example:

Q: There is sequence A=int[]{1,2,3,4,5,6,7,8,0}; B=int[]{2,4,7,8,9}. Request a sequence C containing common values ​​of A and B.

If you follow the original idea, the encoding may be as follows:

illustrate:

List<int> c = new List<int>();
foreach(int a in A){

  foreach(int b in b) {

    if (a==b) {
      (a);
    }

  }

}

illustrate:

Do you think the above paragraph is not a problem, but it is ugly. What if we quote LINQ to write:

IEnumerable<int> C = from a in A
           from b in B
           where a==b
           select a;

grammar

1. The main namespace where LINQ is located:

2. The core object of LINQ processing is the IEnumerable enumerable object, which also includes generic enumerations. In other words, when the object you want to process is an IEnumerable type object, you can use LINQ to operate it. And without other processing, a new IEnumerable sequence will be returned. Note that LINQ has a feature "delayed loading" which will be explained later.

3. Keywords (excerpted from MSDN):

from :       Specify the data source and scope variables (similar to iterative variables).

where:   Filter source elements based on one or more Boolean expressions separated by logical AND and logical OR operators (&& or ||).

select: Specifies the type and form that elements in the sequence returned when the query is executed will have.

group:    Group the query results according to the specified key value.

into:       Provides an identifier that can act as a reference to the results of a join, group, or select clause.

orderby: Sort the query results in ascending or descending order based on the default comparator of element type.

join:        Join two data sources based on an equal comparison between two specified matching conditions.

let:                       Introduce a range variable to store the results of subexpressions in a query expression.

in:                                  Context keywords in the join clause.

on:                    Context keywords in the join clause.

equals:    context keyword in the join clause.

by:                    Context keywords in the group clause.

ascending: context keyword in orderby clause.

descending: context keyword in orderby clause.

4. Syntax description: Each LINQ statement starts with from and ends with select. If this is not the case with T-SQL syntax, remember to think preconceived. Other keywords such as where are similar to T-SQL as filtering and judgment conditions.

Example: IEnumerable<T> nums = from n in nums where .... orderby... select....

Expand

Since .net 3.0, MS has introduced some other new features to us. Due to space, I will briefly introduce some commonly used features of LINQ:

1. Keyword var:

Instructs the compiler to infer the type of the variable based on the expression to the right of the initialization statement. Inferred types can be built-in types, anonymous types, user-defined types, or types defined in the .NET Framework class library. In this way, we can in the above LINQ expression, for example, abbreviated as: var nums = from n in nums where .... orderby... select....

2. Anonymous type:

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without explicitly defining a type first. The type name is generated by the compiler and cannot be used at the source code level. The type of each attribute is inferred by the compiler. For example: var obj = new {A="a", B="b"}; while LINQ can be var nums = from obj in objs select new {, }

Case

Normal query

var query = from num in num 
      select 

Filter query

var query = from obj in objs
      where  > Condition
      select obj

Group query

var query = from obj in objs
      group obj by  into g
      orderby 
      select g;

Note that in this example, the keyword into is not necessary. When using into, you must continue to write the query and end the query with a select statement or another group clause.

Inline query

var query= from obj1 in objs1
      join obj2 in objs2 on  equals 
      select new { A= , B =  };

Left Outreach Query

var query = from obj1 in objs1
      join obj2 in objs2 on  equals obj2.Obj1ID into g
      from subpet in ()
      select new { P1 = obj1.P1, P2 = (subpet == null ? null : subpet.P2 ) };

Note that the static extension method of .net 3.5 new feature is involved (the subsequent introduction does not affect understanding) DefaultIfEmpty(): If the sequence is empty, a single instance collection with default values ​​is returned.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.