Get the App
SLTechnology News&Howtos  ›  Development  › 

Basic Analysis of LINQ query Syntax

Shulou Source: shulou.com Published: 2022-06-03 19:16:03 10月25日 Update

This article introduces the relevant knowledge of "basic analysis of LINQ query syntax". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Set initializer

Public class Person {public string username {get; set;} public int age {get; set;} public override stringToString () {return string.Format ("username: {0} age: {1}", this.username, this.age);} var persons = new List {new Person {username = "a", age=1}, new Person {username = "b", age=2}}; foreach (var p in persons) Console.WriteLine (p.ToString ())

The compiler will automatically insert the collection for you. If you initialize for Hashtable, you are using two object initializers.

LINQ query syntax

Var persons = new List {new Person {username = "a", age=19}, new Person {username = "b", age=20}, new Person {username = "a", age=21},}; var selectperson = from p in persons where p.age > = 20 select p.username.ToUpper (); foreach (var p in selectperson) Console.WriteLine (p)

LINQ query syntax is a convenient declarative simplification when using standard LINQ query operators to express queries. This syntax can improve readability and simplicity when expressing queries in code, and it is easy to read and write correctly. Visual Studio provides complete intelligent sensing and compile-time checking support for query syntax. At the bottom, the compiler translates the expression of the query syntax into explicit method call code, which is implemented by new extension methods and Lambda expression language features. The above query syntax is equivalent to the following code:

Var selectperson = persons.Where (p = > p.age > = 20) .Select (p = > p.username.ToUpper ())

LINQ query syntax can achieve more than 90% of the function of T-SQL (because T-SQL is based on two-dimensional tables, the query syntax of LINQ is simpler and more flexible than T-SQL), but due to intelligent sensing, select cannot be entered at the beginning.

This is the end of the introduction to the basic analysis of LINQ query syntax. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Queries syntax code compilation basics analysis content methods intelligence more knowledge compilers expressions faceted practical concise learned and then two writing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker MySQL Redmi macOS