One-step learning of the LINQ Series 1 --- What is LINQ ?, Linq1 ---

Source: Internet
Author: User

One-step learning of the LINQ Series 1 --- What is LINQ ?, Linq1 ---
I. goals of this series

1. Understand LINQ;

2. Can write complex LINQ statements (such as dynamic queries );

3. Understand Expression Tree and related concepts;

4. Be proficient in writing beautiful code using LINQ (hope to work together and finally achieve it );

2. What is the content of LINQ?

The competition for the production of LINQ has receded, and now, LINQ has become one of the essential technologies of C # developers. Many people use it to write beautiful code. It has become a completely new way of developing data. This may be one of the benefits of choosing. NET as a development platform. A growing number of open-source libraries and frameworks are using large volumes of LINQ. Whether it is to improve your skills or read other people's code, it is a stone that you and I win. Next, I will introduce it to you and talk about some of your own ideas. If you have any questions, please correct them.

1. What is LINQ?

A common, language-integrated query tool. With this tool, you can access data from a variety of data sources, such as memory Objects (LINQ to Objects), databases (LINQ to SQL), XML files (LINQ to XML), and file systems. It is a set of functions introduced in Visual Studio 2008. It provides powerful query functions for C # and Visual Basic syntax, and has withstood the test and accumulation of time.

The following describes the traditional method of querying data in the memory object data source and the writing method of LINQ:

 

/// <Summary> /// elements smaller than 5 in the output array /// </summary> public static void RraditionalMethod () {int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; // the conventional method uses the cyclic List <int> lowNums = new List <int> (); for (int I = 0; I <numbers. length; I ++) {if (numbers [I] <5) {lowNums. add (numbers [I]) ;}} Console. writeLine ("Numbers <5:"); foreach (var x in lowNums) {Console. writeLine (x) ;}/// <summary> // elements smaller than 5 in the output array /// </summary> public static void LinqMethod () {int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // write var lowNums = from num in numbers where num <5 select num; Console. writeLine ("Numbers <5:"); foreach (var x in lowNums) {Console. writeLine (x );}}

Maybe you think that the method of querying data in the memory object data source does not give me any benefit (at least from the example, the number of codes is similar ). Don't worry! Let's take a look at the operations on the data in the database and then discuss how to use the SQL statement to query the persistent data in the database (relational database here) before we can use LINQ, when writing code, the red (the color of the string in VS) SQL statement will certainly bring you a lot of trouble? Maybe it's a wrong spelling of a letter, maybe it's wrong alphabetic order, maybe ......, The compiler did not give us any prompt. As a result, an exception occurs during running. In a complicated system, it takes several hours to solve this problem, and even worse, the same error often occurs !!! However, when we use LINQ to SQL, we will not bother you any more. Please refer to the following example to see if you are not feeling at your heart.

/// <Summary> /// query the traditional writing method of the order number less than 5 /// </summary> /// <param name = "connectionString"> </param> private static void ReadOrderData (string connectionString) {string queryString = "SELECT * FROM dbo. orders WHERE OrderID <5; "; using (SqlConnection connection = new SqlConnection (connectionString) {SqlCommand command = new SqlCommand (queryString, connection); connection. open (); SqlDataReader reader = command. executeReader (); try {while (reader. read () {Console. writeLine (String. format ("{0}, {1}", reader [0], reader [1]) ;}} finally {// close the object reader after reading. close ();}}}

 

/// <Summary> /// query the LINQtoSQL statement for the order number less than 5 // </summary> public void ReadOrderDataByLinqToSql () {var q = from c in db. orders where c. orderID <5 select c; ObjectDumper. write (q );}
Iii. Why use of LINQ

From the comparison above, we can see that LINQ will bring us the following benefits:

1. Integration: it encapsulates the query into the C # (VB. NET) syntax and obtains the same highlighted display and type check as other C # (VB. NET) syntaxes.

2. the query logic is clearer.

3. Use unified query syntax to process data in data sources such as memory objects, databases, and XML;

4. Improved development speed (the above linq to SQL example );

5. Improve code maintainability and scalability (will be introduced later );

In addition, linq to SQL can effectively prevent SQL injection and improve system security. It's not early today. Wash your bed first to prevent death. If it is helpful to you, please click recommendation support to increase the motivation for subsequent writing. If anything is inappropriate, please correct it again.

Related Article

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.