Entity Framework-linq

Source: Internet
Author: User

LINQ is an important breakthrough in. NET, and the main purpose of LINQ is to use it as a generic search language in. Net.

The purpose of LINQ design is to check the inquiry, not including the addition of new changes to delete and other material features, so LINQ in EF can only be used as a search, the other parts of the LINQ does not have any relationship.

The best thing about using LINQ is that LINQ is a strong, object-based approach to language, so it's easy to make sure that the language is problematic at the time of editing.

For LINQ to have a concept, LINQ itself is designed to be available across different sources, such as xml,dataset ... Wait a minute.

So different sources will have a response to the LINQ Library, which is now built in. The LINQ Libray in the. Net.

1.Linq to Object

2.Linq to XML

3.Linq to ADO

And the source of his information, LINQ offers an outreach. For example, the network can find the WMI to LINQ library, if the ability can also be self-design expansion.

So the same is for EF, so EF has also expanded to support LINQ.

There are two ways in which LINQ writes

1.Linq Language method

List<customer> customers = ...; var  from inch Customers         where 1;

2.Lambda expression

List<customer> customers = ...; var 1);

The final results of both writes are the same, and the performance is the same, but the 2nd is more resilient to the operation of LINQ.

The expansion of LINQ in EF is more complicated than other sources.

Underneath the structure of the EF

Since EF does not have a kidnapped repository on its structure, EF provides the EntityClient data provider concept, and data provider ultimately accesses the repository through ADO.

So when one is not. Net native Support (currently only MSSQL) is required to support EF must provide EntityClient Data Provider outside of the ADO Provider.

The main purpose of EntityClient Data provider is to translate LINQ and Entity SQL query into ADO Command (SQL).

Therefore, LINQ does not support all features in EF. It is necessary to see if EntityClient Data provider has (CAN) translate the LINQ language into ADO Command (SQL).

And for LINQ,EF has already defined some standard ANSI generic inquiry response. For example, the CONTAINS function corresponds to the like language method.

var q = dbContext.Customers.Where (c = c.name.contains ("XXX"));

But since there is no kidnapped in the EF design, EF offers additional outreach to specific features of some specific repositories. "--"

For example, Ms-sql can use specific methods of sqlfunctions in Ms-sql, such as

var q = dbContext.Customer.Where (c = Sqlfunctions.datediff ("SS"0);

The above example goes through the DateDiff method in sqlfunctions to respond to the DATEDIFF function in SQL language, which, of course, also represents the kidnapped of Ms-sql.

In addition to the user Define function (user-defined function) Part of EF also has the means to provide an expansion, and then continue the chapter to explain this part.

Using LINQ in EF often asks questions or bugs.

For example, the code below

var q = dbContext.Customers.Where (c = c.name.contants ("XX") && Check ( c));

The check function is a C # program that is also written to check for conformance, and contains a number of complex logically edits.

As you can see from the above, it's going to be a mistake, because EntityClient Data provider doesn't (and doesn't know) how the check function should be translated into ADO Command (SQL).

So we adjust

var q = dbContext.Customers.Where (c + = c.name.contants ("XX")). ToList (). Where (c = Check (c));

Divide the language into two paragraphs first through. The ToList () method takes the information from the repository back to the memory, because this time the information is already in memory, so the second paragraph of the Where is actually through LINQ to object operation rather than EF.

It is important to note that when you operate EF, you need to know what time it takes to retrieve the information from the repository.

If the above example is written in the following language

var q = dbContext.Customers.ToList (). Where (C.name.contants ("XX") && C = Check (c));

Although the results are the same, but the effectiveness of the difference is very large

Because. The ToList () method pulls the customers information from all repositories back into memory and then performs the Where in the program to do the stuffing.

And the method described above is through where (c = c.name.contants ("xx")) at the time of the inquiry, first use the SQL language to plug the results into memory and then continue to check.

Entity Framework-linq

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.