C # Learning Record 7--linq

Source: Internet
Author: User

LINQ (language-integrated query) language-integrated queries can help us add query functionality to C #. can be used for searches such as databases.

Declaring query is very simple and syntactically similar to the SQL language, but with a little difference

With respect to LINQ, there is 101 samples on MSDN that explains how LINQ is used in specific projects. Links: https://code.msdn.microsoft.com/101-linq-samples-3fb9811b

One, query execution execution of queries

This paper introduces the characteristics of query execution.

Delayed execution of 1.Query

1              Public voidLinq99 ()2             {3 4                 //Queries is not executed until-enumerate over them.5                 int[] numbers =New int[] {5,4,1,3,9,8,6,7,2,0 };6 7                 inti =0;8                 varSimpleQuery =9                      fromNuminchnumbersTen                     Select++i; One  A                 //The local variable ' i ' is not incremented -                 //Until the query is a executed in the foreach loop. -Console.WriteLine ("The current value of I is {0}", i);//i is still zero the  -                 foreach(varIteminchsimplequery) -                 { -Console.WriteLine ("v = {0}, I = {1}", item, i);  +                 } -}

A Qurey is declared in line 8-10. Since there is no where, all elements in the numbers meet the requirements, each time a conforming element is found, ++i

14 Rows of output statements, I is still 0. The reason is that declaring query does not execute immediately

A 16-line foreach statement is the place to actually execute the query statement

Results

v = 1, i = 1
v = 2, i = 2
v = 3, i = 3
v = 4, i = 4
v = 5, i = 5
v = 6, I = 6
v = 7, i = 7
v = 8, i = 8
v = 9, i = 9
v = ten, I = 10

2. Multiple multiplexing of a query (query reuse)

1              Public voidLinq101 ()2             {3 4                 //Deferred execution lets us define a query once5                 //And then reuse it later in various ways.6                 int[] numbers =New int[] {5,4,1,3,9,8,6,7,2,0 };7                 varLownumbers =8                      fromNuminchnumbers9                     whereNum <=3Ten                     Selectnum; One  AConsole.WriteLine ("First Run numbers <= 3:"); -                 foreach(intNinchlownumbers) -                 { the Console.WriteLine (n); -                 } -  -                 //Query the original query. +                 varLowevennumbers = -                      fromNuminchlownumbers +                     whereNum%2==0 A                     Selectnum; at  -Console.WriteLine ("Run lowevennumbers Query:"); -                 foreach(intNinchlowevennumbers) -                 { - Console.WriteLine (n); -                 } in  -                 //Modify the source data. to                  for(inti =0; I <Ten; i++) +                 { -Numbers[i] =-Numbers[i]; the                 } *  $                 //During This second run, the same query object,Panax Notoginseng                 //lownumbers, would be iterating over the new state -                 //of numbers[], producing different results: theConsole.WriteLine ("Second Run numbers <= 3:"); +                 foreach(intNinchlownumbers) A                 { the Console.WriteLine (n); +                 } -Console.WriteLine ("Second Run lowevennumbers query:"); $                 foreach(intNinchlowevennumbers) $                 { - Console.WriteLine (n); -                 } the}

First notice that the WHERE statement that appears in 9, 21 rows will select the result according to certain conditions
The same query statement is used two times in rows 13 and 40. Between two queries, all the numbers in the numbers are taken to the opposite number. The results also changed.

It can be said that creating a query is similar to creating a view (view) inside SQL, which can be used to query the results of a condition .

Results

First run numbers <= 3:
1
3
2
0
Run lowevennumbers Query:
2
0
Second Run numbers <= 3:
-5
-4
-1
-3
-9
-8
-6
-7
-2
0
Second Run lowevennumbers Query:
-4
-8
-6
-2
0

3. Force immediate execution of queries

1              Public voidLinq100 ()2             {3 4                 //Methods like ToList (), Max (), and Count () cause the query to be5                 //executed immediately. 6                 int[] numbers =New int[] {5,4,1,3,9,8,6,7,2,0 };7 8                 inti =0;9                 varImmediatequery = (Ten                      fromNuminchnumbers One                     Select++i) A                     . ToList (); -  -Console.WriteLine ("The current value of I is {0}", i);//I have been incremented the  -                 foreach(varIteminchimmediatequery) -                 { -Console.WriteLine ("v = {0}, I = {1}", item, i); +                 } -}

Unlike example 1, in line 12th, the query uses ToList () to force immediate execution of the current query query. As you can see in Example 2, here is a good place to save a temporary cached result of a database and view the results in the interim.

Results

The current value of I is 10
v = 1, i = 10
v = 2, i = 10
v = 3, i = 10
v = 4, i = 10
v = 5, i = 10
v = 6, i = 10
v = 7, i = 10
v = 8, i = 10
v = 9, i = 10
v = ten, I = 10

Ii. sort of query

1              Public voidLinq28 ()2             {3                 string[] Words = {"Cherry","Apple","Blueberry" };4 5                 varSortedwords =6                      fromWordinchwords7                      byWord8                     SelectWord;9 TenConsole.WriteLine ("The sorted list of words:"); One                 foreach(varWinchsortedwords) A                 { - Console.WriteLine (w); -                 } the}

7 line ORDER By default alphabetically by string from small to large sort
Similar to the following: W. Length is sorted from small to large by string length

Results

The sorted list of words:
Apple
Blueberry
Cherry

Msdn101samples it into a few parts, after the download of each part has a description file, detailing the examples, highly recommended!

C # Learning Record 7--linq

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.