Dlinq getting started series (4)

Source: Internet
Author: User

I apologize to my friends who are interested in dlinq. I have been busy with my recent work and have no time to write a blog. Starting from this section, let's talk about the dlinq syntax. Let's start with the select clause. Let's look at the example below. VaR q =
From C in db. MERs
Select C. contactname;

This is the simplest dlinq query statement. The contact name is obtained from the query. Here, I would like to remind you that, for example, this statement is just a declaration, dlinq does not actually extract the data, only when you need the data, it will help you get it. This is deferred loading ). You can use the tolist () or toarray () method if you want dlinq to help you get the data during the declaration. For example.

VaR q = (from C in db. MERs
Select C. contactname). toarray ();

Or

VaR q = (from C in db. MERs
Select C. contactname). tolist ();

Here, I would like to remind you a little bit. The result set returned by dlinq is a collection of objects, not data.
During dlinq execution, it first converts the above standard query to the dlinq API (also called the cascade method). For example, the following statement

VaR q =
From C in db. MERs
Where C. City = "London"
Select C;

It is first converted to VaR q = dB. MERs. where (C => C. city = "London "). select (C => C); that is, the two statements are equivalent. Then, dlinq parses the shadow file, automatically generates SQL statements based on the query statement of dlinq, sends the SQL statements to the SQL Server, and creates corresponding Objects Based on the returned dataset. Here, you may feel very unfamiliar with C => C. This is a Lambda expression. You can understand C as any object in the result set. The object type is consistent with the element type in your result set. This may be difficult to understand. Let's take a look at the concept that data is an object. I believe this will help us understand lambda expressions.

Prior to dlinq, we had hibernate in the Java field and Nhibernate Technology in the net field to implement object/relational persistence and query services. In essence, dlinq is a tool that is more powerful than many technologies. Data, that is, the meaning of an object, has two layers. First, the data structure (table structure) is a class. It can be described as table schema -- class. Second, the data in the table is a variable, which is described as data -- Object (variable ). Therefore, it may be easier to understand lambda expressions. As we have already said, VAR q = dB. MERs. where (C => C. city = "London "). select (C => C); returns the customers object set. That is to say, each element of this set is a customer. Lambda expressions are extensions of anonymous methods (anonymous method) in C #2.0. It simplifies the implementation of anonymous methods. Here C is an implicit declaration. the compiler will automatically deduce its actual type and display the Declaration. For example, VAR q = dB. MERs. where (customer C) => C. city = "London "). tolist (); lambda expressions follow an expression with the => symbol. This expression needs to return a type. Its essence is that a method returns a type. It is just a more concise anonymous method. Then, operators such as where use the type it returns as a parameter. For the specific implementation of lambda expressions, I will explain in detail in the advanced section. I will not go into details here.

One thing to note is that the standard query statement must be the SELECT statement at the end, and the position of the cascade expressions and operators is not very important. For example, VAR q = dB. MERs. where (C => C. city = "London "). select (C => C); can be written as VAR q = dB. MERs. select (C => C ). where (C => C. city = "London"); the two of them are the same, but the standard query cannot change seats. The SELECT statement must be at the end. Although the positions of operators in the cascading expressions are not very important, they are still different. Especially when anonymous classes are used, the difference is obvious. However, we only need to remember that the next operator is always filtered Based on the dataset filtered by the previous operator. This will be explained in more detail in future blogs.
In the SELECT statement, Another difficulty is the Anonymous class. For example, column

VaR q =
From C in db. MERs
Select New {C. contactname, C. PHONE };

In fact, there are not only anonymous classes in select operations, but also others. Let's take a look at anonymous classes. The preceding statement and
VaR q = dB. MERs. Select (C => New {C, contactname, C. PHONE}); is equivalent. The anonymous class is a new feature in C #3.0. The essence is that the compiler automatically generates an anonymous class according to the user's definition to help users store temporary variables. Note: It is a temporary variable. The extensive use of anonymous classes reduces the readability of the program. The anonymous class also relies on another feature, that is, in C #3.0, objects can be created based on the property. For example, a class

Public class person
{
Private string name;

Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

Previously, we could only use constructors to create its objects. Now in 3.0, property can be used to create objects.
VaR d = new person {name = "S"}; to create an object. Here, you may also have questions about the VaR type. You may think that C #3.0 is of the same weak type as JavaScript. In fact, VaR is not the type of C #3.0. It is the keyword of the compiler. The Compiler automatically deduce the type based on the Type returned by the actual variable. VaR c = NULL; it cannot be compiled because null represents the type. Therefore, C #3.0 is strongly typed.

Now that 3.0 supports using property to create objects, an anonymous class will appear. For example, VAR d = new {name = "S"}; the compiler automatically generates an anonymous class with the property name, allocates memory for the class, and initializes the object. There is another problem here, for example, VAR d = new {"S"}; is not compiled. The compiler does not know the name of the property in the anonymous class. However, if string c = "D"; var d = new {c};, it can be compiled. The compiler will create a property called Anonymous class with C.

In dlinq, such as new {C, contactname, C. PHONE}); here, contactname and phone are the properties defined in the shadow file that correspond to the fields in the table. When the compiler retrieves data and creates an object, it creates an anonymous class which has two attributes: contactname and phone, and then initializes the object based on the data. The anonymous class has another form.

VaR q =
From E in db. Employees
Select New {name = E. firstname + "" + E. lastname, phone = E. homephone };

In this form, the compiler renames the property name. Of course, the two forms can also be combined.

VaR q =
From P in db. Products
Select New {P. productid, halfprice = P. unitprice/2 };

The name of the first attribute will not change, and the second attribute will be renamed.
Well, let's talk about these first. I will introduce several more complex usage in the next section.

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.