Visual Basic 9.0 cutting-edge broadcast-static Article (5) query includes

Source: Internet
Author: User

The new features of Visual Basic 9.0 are originally designed to cater to the. NET Framework's new data Framework-Linq. One of the slogans of Linq is "Making queries everywhere". It introduces SQL-like syntax to Visual Basic and can be used in any combination. Do not just write the database SQL into VB, so it is too small to look at Linq. The goal of VB is to allow all objects containing data to be queried using SQL-like queries, including database LINQ ing objects (DLinq), XML (XLinq), and even all set objects in. NET Framework.

Let's look at an example. Assume that the Employee class has attributes such as FirstName, LastName, and Age. Now there is a List (Of Employee) type set called emp. Suppose we want to query the names of all employees whose Age is greater than 25. Recall how the features we introduced previously bring new changes to queries. First, we add the extension method Where to IEnumerable:

 <Extension()> _Public Function Where()Function Where(Of T)([Me] As IEnumerable(Of T), predicate As Func(Of T, Boolean))_As IEnumerable(Of T)Dim result As New Collection(Of T)For Each Dim item In [Me]If predicate(item) Then result.Add(item)NextReturn resultEnd Function

Note Func. This is. NET Framework will add a set of generic delegates for On-the-fly to create the number and type of delegates for various parameters without declaring a new delegate type. Here Func is used to indicate an asserted. Then we encapsulate the condition query. At the same time, we can encapsulate a Select method for data type conversion, and so on, as well as OrderBy and GroupBy ...... These become the query operators of Linq.

Recall that we also have nested functions, so we can use the query operator using the following methods:

 Function F()Function F(obj As Employee)As BooleanReturn obj.Age > 25End FunctionDim result = emp.Where(AddressOf F)

What is a little troublesome here is that we need to define our own functions so that we will be very tired when performing complex queries. Even if Lambda expressions may help us, we also hope to find a method that is easier to understand, that isQuery includes Query Comprehension. The newly introduced query keywords such as Select and Where can help you complete the entire set of content described above:

 Dim result = Select It From e In emp Where e.Age > 25

A simple SQL-like syntax helps you generate anonymous nested functions and call of all operators. Note the It usage. Select It indicates selecting all fields. We only need the Employee name, so we can write it like this:

 Dim result = Select e.FirstName, e.LastName From e In emp Where e.Age > 25

In this way, the Select statement converts the query result into a Tuple (anonymous instance), which only contains the fields you need. Then you will find that the power of query can extend to all the data you can access. This relational syntax will soon replace the manual iteration of For Each.

We know that VB9 will have excellent improvements in XML. When XML literal and XML are combined with the syntax contained in the query later, it will show extraordinary conciseness and efficiency, this is incomparable to C #3.0 and other languages in the same period. We will continue to introduce the XML features.

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.