LINQ (Language Integrated Query) (1): Introduction

Source: Internet
Author: User

C #2.0 the biggest improvement is generics. the biggest improvement of C #3.0 is LINQ (Language integration query ). there are three major segments in the Table: LINQ to XML and LINQ to ADO. net (LINQ to SQL, LINQ to dataset, LINQ to entity), LINQ to object.

Disputes over LINQ

Some of you have made changes to LINQ to ADO. NET may have many objections. I think it is for ADO.. NET is encapsulated again, reducing the performance. and ADO. net. why is it so troublesome to learn another new technology, LINQ. in fact, some newly added features in programming languages are often used to facilitate developers, improve development efficiency, and reduce the possibility of making mistakes. of course, you have to consider performance at the same time. but now the computer hardware configuration is getting better and better. in the past, the memory was only several hundred MB, but now it's just a few GB, And the CPU performance is getting better and better. so sometimes don't worry too much about computers. for programmers. how to write less code to implement more functions. there is no need to worry too much about complicated operations to be executed after simple code. hosting code doesn't mean you do a lot of things, for example, you don't have to worry about memory leaks. the process of adding a CLR in the middle naturally degrades. but you have written code to save time.

Of course, if you think that the performance of LINQ is not good, you can use other methods to implement the functions, but write more code.

Simple Example

First, let's give a few simple examples about the LINQ to object syntax and see how to use it.

String [] names = {"Arwen", "James", "Sunny", "Lily", "ADA "};

VaR name = from Na
In names

Select NA;

Foreach (var str in name)

{

String S = STR;

Console. writeline (s );

}

Analysis:

First, let's talk about the keyword var, which is used for type inference. That is to say, it can be used to define a type instead of all the keywords such as string and Int. then, the compiler can deduce the variable type through the variable value during compilation.

In fact, here you can also use ienumerable <string> name to replace var name to define variables.

Then there is a keyword like "from" and "select". It looks like the syntax of an SQL statement.The Design of LINQ should refer to the concept of SQL.The SQL statement encapsulates a lot of details,Only tell the computer what to do, instead of telling him how to do it.. We usually use the C #, Jave, and C ++ languages to not only tell computers what to do, but also how to do it. write each step in code. in the preceding example. if we want to obtain the elements starting with a in the array. if you do not use LINQ, you must write code to judge it.

List <string> List = new list <string> ();

Foreach (string s in names)

{

If (S. startwith ("")

List. Add (s );

}

That is to say, You need to specify the specific method for the computer, but if you use LINQ, you can directly use the following statement:

VaR name = from Na in names

Where na. startswith ("")

Select NA;

Use a where statement to tell the computer what to do.

Therefore, the true significance of LINQ is not only to make some of your operations simpler, but also to introduce the idea of SQL statements. Try to encapsulate some specific operation details and focus on the functions to be implemented by the software.. Of course, the application scope of SQL statements is limited, only for some simple table operations, more complex operations also need some expansion, such as SQL Server T-SQL, Oracle PL/SQL, in the face of some complex operations, they have to write a lot of code to tell the computer how to do it.

The query language is also limited in the same way in LINQ. You can only query data by name. Therefore, you can only retrieve data from the array names, rather than modifying the data in the array through LINQ.

Many of the usage of LINQ is similar to that of SQL, so you can refer to the usage of SQL keywords. Of course, there are other more complex operations in LINQ. Let's take a simple example to explain how to sort the obtained data.

String [] names = {"Arwen", "James", "Sunny", "Lily", "ADA "};

VaR name = from Na in names

Orderby na descending

Select NA;

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.