There are two types of syntaxes available for you to use when writing a LINQ query: query syntax and method syntax.
1. Introduction to query syntax and method syntax?
Query syntax: it is declared and looks similar to an SQL statement. The query syntax is written in the form of a query expression.
Method Syntax: it is in the Command Format and uses standard method calls. A method is a set of methods called standard query operators.
Note: 1> you can use a combination of the two methods in a query.
2> the query syntax used by the query expression is converted by the C # compiler into a method call. There is no difference in runtime performance between the two methods.
2. How to Use the query syntax and method syntax in a LINQ query?
For example:
1 class Program
2 {
3 static void main (string [] ARGs)
4 {
5 Int [] numbers = {2, 5, 28, 31, 17,16, 42}; // declare an array as the data source.
6
7 var numsquery = from N in numbers // query syntax (query expression)
8 where n <20
9 select N;
10
11 var numsmethod = numbers. Where (x => x <20); // method syntax (standard query operator)
12
13 int numscount = (from N in numbers // a combination of two forms
14 where n <20
15 select N). Count ();
16
17 // traverse the set numsquery
18 foreach (var x in numsquery)
19 {
20 console. Write ("{0},", X );
21}
22 console. writeline ();
23
24 // traverse the set numsmethod
25 foreach (var x in numsmethod)
26 {
27 console. Write ("{0},", X );
28}
29 console. writeline ();
30
31 console. writeline ("{0}", numscount );
32
33 console. readkey ();
34}
35}
36 /*
37 program output results: 2, 5, 17, 16
39 4
40 */
The preceding examples provide a simple understanding of the query syntax and method syntax. Later, we will study the query syntax and method syntax in depth based on the actual LINQ query. We will summarize it here today, I will continue to write the structure of the query variables and Query expressions tomorrow night. Oh, it's almost. I have to take a break early and I have to go to work tomorrow!