C #3.0-LINQ (1)

Source: Internet
Author: User

Language integrated query. Many people are familiar with the SQL language and it is very convenient to use it to operate the database. Now, in C #3.0, you can use the same syntax to operate various types of data, including arrays, files, and databases. As there are many things in LINQ, I want to talk about it three times. For the first time, I will mainly talk about LINQ. For the second time, I will mainly talk about using LINQ to operate databases. For the third time, I will introduce how to use LINQ to operate XML, the fourth section mainly introduces some function applications of LINQ. I have time to write some special topics about LINQ.

The following is an example in which a programmer should talk with the code:

Class Program
...{
Static void main (string [] ARGs)
...{
String [] names =... {"Everett", "Albert", "George", "Harris", "David "};
VaR items = from name in names
Where name. length> = 6
Orderby name
Select name. toupper ();
Foreach (VAR item in items)
Console. writeline (item );
}
}

Are you familiar with the from, where, orderby and other words above? We can easily query some arrays like a database. In fact, the above expression is equivalent to the following statement:

VaR items = names. Where (name => name. length> = 6). orderby (name => name). Select (name => name. toupper ());

So why can we apply these methods on arrays? These methods are not available on arrays? Let's recall what we said, the extension method, right. here we can see the application of the extension method. We can also see that the application of lambda expressions makes the statements more concise and easy to understand. Where are these extension methods defined? We can see this definition in the system. LINQ assembly:

Namespace system. LINQ ...{
Public static class enumerable ...{
Public static ienumerable <t> where <t> (
This ienumerable <t> source, func <t, bool> predicate )...{

Foreach (T item in source)
If (predicate (item)
)
Yield return item;
}
}
}

Here we can clearly see many this keywords, which are the symbols of the extension method. If we are not very familiar with Lambda expressions, the above LINQ statements can be further converted into the form of delegation.

Func <string, bool> filter = delegate (string s)... {return S. length> = 6 ;};

Func <string, string> extract = delegate (string s)... {return s ;};

Func <string, string> project = delegate (string s)... {return S. toupper ();};

VaR items = names. Where (filter). orderby (extract). Select (project );

There is a problem. We will notice that VaR is used as its type before items. Is there any question about the type of VaR? Let's take a closer look at the system. LINQ assembly and we will find that the expansion method returns the ienumerable <t> type, a generic interface. That's right, VaR is the generic interface. Another problem is that when names meets certain conditions, we can use the LINQ expression for query. This should be carefully observed from that Assembly. We will find that there is a source parameter. According to the syntax of the extension method, we know that this source parameter is the object that calls this method. We can infer that this object must be converted to ienumerable <t>. So what type can be converted successfully? Obviously, only the type of the generic interface ienumerable <t> can be converted. For example, the array type, list <t>, and so on. When we can also define the type, as long as this interface is implemented, we can operate on this type with LINQ.

This is just a brief introduction to LINQ. In the future, we will talk about some specific applications, but we will see some powerful functions of LINQ from the introduction.

 

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.