C # 3.0 Getting Started series (iv)-select operation

Source: Internet
Author: User
Tags anonymous net domain

First to pay attention to DLinq friends apologize, recently work really busy, no time to write a blog. Starting from this section, we'll talk about DLinq grammar. Let's start with the SELECT clause. Look at the example below.

var q =
From C in DB. Customers
Select C.contactname;

This is the simplest DLINQ query statement, the query gets the name of the contact person. Here, I need to remind you that, like this statement is just a statement, DLinq does not really take out the data, only when you need the data, it will help you to fetch, this is deferred loading (deferred loading). If you want the dlinq to help you get the data at the time of the statement, you can use the ToList () or ToArray () method. As in the previous example.

var q = (from C in DB. Customers
Select C.contactname). ToArray ();

Or

var q = (from C in DB. Customers
Select C.contactname). ToList ();

Here, I would also like to remind you a little bit. The result set returned by DLinq is a collection of objects, not data.

When DLinq executes, it will first convert the standard query above into a DLINQ API (also known as a cascade method), such as the following statement

var q =
From C in DB. Customers
where c.city = "London"
Select C;

will be converted to var q = db first. Customers.where (c=>c.city== "London"). Select (C=>C); In other words, the two statements are equivalent. Later, DLinq parses the mapping file, automatically generates the SQL statement according to the DLINQ query statement, and sends SQL to the SQL Server server, creating the corresponding object based on the returned dataset. Here, you may feel very strange to c=>c. This is a lambda expression (expression), and you can understand that C is any object in the result set, and that the type of object is consistent with the element type in your result set. It may be difficult to understand here. Let's understand that data is the concept of an object. I believe this will help us understand lambda expressions.

Prior to DLinq, there were hibernate in the Java domain and NHibernate technology in the net domain to achieve object/relational persistence and query services. DLinq, in essence, is a tool that is more powerful than them, based on the absorption of many technologies. The data is the meaning of the object has two layers. First, the Data structure (table structure) is the class. can be described as table Schema--class. Second, the data in the table is the variable, described as data--object (variable). So it might be easier for us to understand lambda expressions. As we have just said, var q = db. Customers.where (c=>c.city== "London"). Select (c=>c); Returns a collection of customers objects, which means that each element of the collection is a customer. A lambda expression is an extension of the anonymous methods (anonymous method) in C # 2.0. It simplifies the implementation of anonymous methods more. The c here is an implicit declaration that the compiler automatically infers its actual type, or it can display a declaration, for example, var q = db. Customers.where (Customer c) => c.city = "London"). ToList (); Lambda expressions follow an expression with => notation, which needs to return a type, essentially a method that returns a type. It is simply a more concise method of anonymity. Then, the where operator uses the type returned by it as a parameter. For a specific implementation of lambda expressions, I will explain them in detail in the Advanced section. Don't repeat it here.

One thing to remind everyone is that the standard query statement must be a SELECT statement at the end, and a cascade expression, where the various operator positions are not important. For example, var q = db. Customers.where (c=>c.city== "London"). Select (C=>C); can be written as var q = db. Customers.select (c=>c). Where (c=>c.city== "London"); the two are the same, but the standard query cannot be changed, and the SELECT statement must be at the end. Although the position of the various operators is not important in a cascade expression, there is a difference. Especially after using anonymous classes, the distinction is obvious. But same, we just remember that the next operator is always filtered on the basis of the dataset filtered by the last operator. This, I will in the future blog, a more detailed description.

Another difficulty in the SELECT statement is the anonymous class. Like an example.

var q =
From C in DB. Customers
Select New {c.contactname, c.phone};

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.