Entity Framework entitysql type

Source: Internet
Author: User
Tags lenovo

EDM (Entity Data Model) defines many types. We can roughly classify it into three types: the first type is the original data type, such as EMD. int32. The second type is the type defined in the schema, such as the object type and link type. The third type is the temporary type, such as set, row, and reference. They are all anonymous types.

The first type and the second type have been widely used. Let's take a look at what is the temporary type.

First, why do they come down to the temporary type? As you know, EF uses object models, while SQL and other databases are relational models. to transfer data between them, there must be a conversion from a relational model to an object model. In addition, the data exchange between relational models can also be carried out directly using these types without bypassing the circle. Once the interaction ends, these types of data mean that the objects will die. Therefore, these types are classified as one type.

What data types does this category contain?

1. Collection)

2. Row)

3. Reference (REF)

So, how to use these types? What are their application scenarios?

Let's start with the set and look at their syntax and typical scenarios.

Set:

Set is created using Multiset () or curly braces -- {}, for example:

Multiset ("Lenovo", "HP", "Asus ")

This is equivalent to: {"Lenovo", "HP", "Asus "}

Here we will give you a wake-up note: the braces behind Multiset are not written as: Multiset {Lenovo, HP, Asus} -- secretly telling everyone, that's exactly what I wrote at the beginning -. -|

Typically, we can use a set to return a series of values. For example:

SelectBrandFrom Multiset("Lenovo", "HP", "Asus ")AsBrand

This will return three string-type rows with one brand name per line.

Max (Multiset("Lenovo", "HP", "Asus "))

The string that returns the largest value, Lenovo, is sorted by letters.

 

Row:

The concept of a row is relatively simple. It is constructed with the keyword row to form a row object. For example:

Row("Lenovo"AsBrand, "t61"AsType)

A row object consisting of the brand and type columns is returned.

A combination of rows and sets can return a set of multiple rows:

Multiset(Row("Lenovo"AsBrand, "t61"AsType ),Row("HP"AsBrand, "v3911tu"AsType ))

The returned results are as follows:

Here, the column name is the alias of the first row object. The following statement is equivalent to the preceding statement:

Multiset(Row("Lenovo"AsBrand, "t61"AsType ),Row("HP"AsAaa, "v3911tu"AsBbb ))

 

Reference:

Reference is equivalent to a pointer in a database. There are two ways to create a reference.

First, use the ref keyword. Let's assume that:

Select ValueNBFromNbwhentities. NotebookAsNB

The collection of all Notebook objects will be returned:

Then, add the keyword Ref:

Select ref(NB)FromNbwhentities. NotebookAsNB

We will return a reference set:

Second, use the createref keyword. For example:

Createref(Nbwhentities. Notebook,Row(1 ))

Here, we use the row keyword to pass in the value of the primary key of the row to be referenced.

So, what is the need for us to use references? We can regard reference as a lightweight solution for getting data. We record where to get the value, but there is no real data to get it. In this way, when it is not necessary, there is no need to transmit large amounts of data between applications and databases. In the preceding statement, we did not retrieve all values.

Once necessary, you only need to access any attribute of this entity set or use the deref expression to obtain data.

For example, the following statement returns Lenovo ThinkPad because its primary key is 1 and we call the brand attribute. It is no longer a simple reference, but actually transmitted data.

Createref(Nbwhentities. Notebook,Row(1). Brand

To obtain the entire row, you can use deref:

Deref(Createref(Nbwhentities. Notebook,Row(1 )))

The result is:

This statement looks funny, but you can use it like this.

In fact, if we change the form, this statement is not so funny:

Deref(Createref(Nbwhentities. Notebook,Row(@ Notebookid )))

In practical applications, we often need to obtain an object based on the primary key. In this case, the input primary key is parameterized and the entire object is returned. This is also a typical application.

Summary:

This article introduces three types of entitysql (in fact, it seems that only one type of pai_^ is introduced ). Maybe everyone thinks this is useless. In fact, it is like learning int and string in programming languages. It cannot be used to produce any benefits. However, they are indeed the most basic concepts in programming.

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.