Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the syntax of LINQ query?

Shulou Source: shulou.com Published: 2022-06-02 09:02:09 09月20日 Update

Editor to share with you what the syntax of LINQ query is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Lambda expression

Var list = new [] {"aa", "bb", "ac"}; var result = Array.FindAll (list, s = > (s.IndexOf ("a") >-1)); foreach (var v in result) Console.WriteLine (v)

In fact, it is similar to the anonymous methods in 2.0, which are used to generate inline methods, except that the syntax of Lambda expressions is more concise. The syntax is as follows:

(parameter list) = > expression or statement block

Where:

Number of parameters: can have multiple parameters, one parameter, or no parameters.

Expression or statement block: this part is the implementation part of the function that we usually write (function body).

Here is a complex example combined with the extension method:

Public delegate int mydg (int a, int b); public static class LambdaTest {public static int oper (this int a, int b, mydg dg) {return dg (a, b);}} Console.WriteLine (1.oper (2, (a, b) = > a + b)); Console.WriteLine (2.oper (1, (a, b) = > a-b))

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, LINQ query syntax is simpler and more flexible than T-SQL), but due to intelligent sensing, select cannot be entered at the beginning.

The above is all the content of the article "what is the syntax of LINQ query?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Query syntax expression parameter method code article grammar conciseness content function intelligence statement faceted compilation complex ordinary almost not much number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology MySQL Microsoft Shulou Tech Info