Linq --- I'll explain the order for you. Your beauty is not only a query statement, but also a write by using

Source: Internet
Author: User

Linq --- I'll explain the order for you. Your beauty is not only a query statement, but also a write by using

This is a group of extensions for the c # and Visual Basic languages. It allows you to write C # Or Visual Basic code to query memory data in the same way as a database. LINQ is short for Language Integrated Query. Translation into Chinese is Language Integrated Query, which is a feature Integrated in. NET programming Language. It has become an integral part of programming languages. During programming, you can get good syntax check, rich metadata, smart sensing, static type, and other advantages of strong language. In addition, it also enables queries to conveniently Query Information in the memory, not just external data sources. She provides a variety of query methods, such as select, where, orderby, and groupby. Do you think you have seen nie somewhere? Oh, in a dream, joke. It's similar to the SQL we 've learned before, what is the difference between them, nie? Let's give an example.

For example, to obtain the number greater than 50 from the numbers array, we write the code as follows:

<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> using System; using System. collections; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace ch01 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnT Est_Click (object sender, EventArgs e) {int [] arr = {123, 12, 3, 12, 15, 4, 6, 46, 48, 56,785 }; // obtain the number greater than 50. // What should we do without LinQ? ArrayList result = new ArrayList (); for (int I = 0; I <arr. length; I ++) {if (arr [I]> 50) {result. add (arr [I]) ;}// print the result and OK for (int I = 0; I <result. count; I ++) {Console. writeLine (result [I]) ;}}</span> </span>

The running effect is as follows:

Next, let's take a look at how our code writes nie if we use linq:

<Span style = "font-size: 18px;"> using System; using System. collections; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace ch01 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnTest_Click (object sender, EventArgs e) {int [] arr = {123, 12, 3, 12, 15, 4, 6, 46, 48, 56,785 }; // With LinQ, we can do this. // obtain the number of IEnumerable ie = arr. select (p => p ). where (p => p> 50); // output -- this part of code can be reused IEnumerator result = ie. getEnumerator (); while (result. moveNext () {Console. writeLine (result. current) ;}}}</span>

The running effect is as follows:

From the above comparison, we can easily see that the use of linq is more concise. Next, let's briefly introduce the extension method of linq. What is the extension method? How does the extension method appear? . Net framework provides many classes and methods for programmers. net framework provides us with many methods in the class, and sometimes it still cannot meet our needs. For example, if you want the string object to have the ToPascal method, the meaning is to convert the string to Pascal format and return it. We know that ,. the String class provided by net framework does not provide corresponding methods for us. What should we do to achieve our goal? Some people say that the String class can be inherited. This is not acceptable because the classes provided by the. net framework are all finnal classes and the final classes cannot be inherited. How can this problem be solved? The extension method is displayed. What are the advantages of the extension method? Provides some additional methods for existing classes. The advantage of doing so is that the original classes do not need to be re-compiled and generated. You only need to introduce some additional dll in the program, let's look at a specific example.

For example, we have a string. When we define a variable, for example, we give it a value and there is a set of methods in the s string, this set of methods is defined by the system for us, if one day, I want to use a special method, such as topuuer to convert to uppercase, tolower to lowercase, and then I want to Use lowercase letters after uppercase. What should I do, there is no such method here. What should I do? How can I implement this function? This is the problem to be solved in our extension method, the purpose of the extension method is to provide additional methods for the existing class to enhance the function of the class. We can do this and add an extension method to it. First, let's create a static class, the extension method is a special static method. How can we implement it:

<Span style = "font-size: 18px;"> using System; using System. collections; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace ch01 {public partial class Form1: Form {public Form1 () {InitializeComponent () ;}} private void btnExtraMethod_Click (object sender, EventArgs e) {// extended method, objective: To provide additional methods for existing classes to enhance the function of the class string s = "asEdSadaDsdSsa"; Console. writeLine (s. toUpper (); Console. writeLine (s. toLower (); Console. writeLine (s. toPascal (); Console. writeLine (s. toPascal (2);} // Method for converting a string's handwritten letters into uppercase letters public string toPascal (string s) {return s. substring (0, 1 ). toUpper () + s. substring (1 ). toLower () ;}</span>
The running effect is as follows:

Finally, let's briefly introduce the lambda expression, the basic expression form of C # Lambda: (parameter list) =>{ method body}, to explain, the parameter type in the parameter list can be a clear type or an inference type. If it is an inference type, the Data Type of the parameter will be automatically inferred by the compiler according to the context, lambda is used in method-based LINQ queries as a parameter for standard query operator methods such as Where and Where. Let's look at a specific example:

<Span style = "font-size: 18px;"> using System; using System. collections; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; public partial class Form1: Form {// defines a delegate public delegate string deleTransfer (string s); public Form1 () {InitializeComponent ();} private void btnTest_Click (object sender, eventArgs e) {// extended method ---- string strTest = "asdsad"; Console. writeLine (strTest. toLower (); Console. writeLine (strTest. toUpper (); Console. writeLine (strTest. toPascal (); Console. writeLine ("-----------------------------------"); // Lambda source //. net FrameWork 1.0 delegate --- function pointer deleTransfer trans = new deleTransfer (ToPascal); // delegate to method ToPascal Console. writeLine (trans ("abcdEFGH "));//. net 2.0 anonymous method deleTransfer transs = delegate (string s) {return s. substring (0, 1 ). toUpper () + s. substring (1 ). toLower () ;}; Console. writeLine (transs ("abcdEFGH "));//. net 3.5 anonymous method // deleTransfertransss = (s) => (s. substring (0, 1 ). toUpper () + s. substring (1 ). toLower (); deleTransfer transss = s => s. substring (0, 1 ). toUpper () + s. substring (1 ). toLower (); Console. writeLine (transss ("abcdEFGH");} // Method for converting the first letter of a string to an uppercase letter public string ToPascal (string s) {return s. substring (0, 1 ). toUpper () + s. substring (1 ). toLower () ;}} public static class ExtraClass {// extended method, special static method public static string ToPascal (this string s) // this post-belt type, the table name adds a special method {return s. substring (0, 1 ). toUpper () + s. substring (1 ). toLower ();} public static string ToPascal (this string s, int len) // this is of the back type, and the table name adds a special method {return s. substring (0, 1 ). toUpper () + s. substring (1, len ). toLower () + s. substring (len + 1) ;}}</span>
Its development process is the delegate used in C #1.0, the anonymous method is added in C #2.0, and the lambda expression is added in C #3.0.

Editor's note: this blog post briefly introduces some basic knowledge about linq, including the comparison of linq and SQL, the extension methods of linq, and lambda expressions, the understanding of linq is not very profound. We welcome our friends to discuss and exchange. An important function of Linq is the implementation of ORMapping. ORMapping is the idea of realizing True Object-oriented programming, so that we don't need to care about the database, just the relational entity class or the entity collection class. The project is in progress and is not yet resumed ......

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.