Entity Framework-LINQ, framework-linq
As an important breakthrough in. Net, the main purpose of Linq is to use it as a general lookup statement in. Net.
The purpose of the Linq project is to allow users to query objects without the addition, modification, division, and other resources. Therefore, in EF, Linq can only be used as a query object, none of the other parts is related to Linq.
The best thing to do with Linq is that Linq is an object-based Dynamic lookup method, therefore, it is easy to confirm whether there is a problem with the validation method during the renewal period.
For Linq, there must be a concept. In terms of programming, Linq can come from different sources, such as XML, DataSet, and so on... And so on.
Therefore, different data sources will have corresponding Linq Library. Currently, it is built in. Net's Linq Libray.
1. Linq to Object
2. Linq to XML
3. Linq to ADO. Net
In addition, as the source of its resources, Linq provides an extension method. For example, you can find related libraries such as WMI to Linq on the Internet. If you have the ability, you can design your own extension.
Therefore, for EF, EF is also supported by Linq.
There are two ways to create a write-in for Linq:
1. Linq flood Method
List<Customer> customers = ...;var q = from c in customers where c.Id == 1;
2. Lambda Representation
List<Customer> customers = ...;var q = customers.Where(c => c.Id == 1);
The last results of these two methods are the same, and the performance is also the same. However, we recommend that you use 2nd methods, which will be more efficient for the operation of Linq.
In EF, the ratio of the browser to other resources is higher than that of the browser.
The following is the architecture of EF.
Since EF does not have any fixed information on the architecture, EF provides the EntityClient Data Provider concept, while the Data Provider is mostly through ADO. net to access data sources.
Therefore, for a non-. Net native support (currently only MSSQL) resource to support EF, you must provide ADO. Net provider and EntityClient Data Provider.
The main purpose of EntityClient Data Provider is to compile the Command (SQL) of ADO. Net between Linq and Entity SQL Query ).
Therefore, not all functions are supported in Linq in EF. it is required to check whether EntityClient Data Provider has (CAN) merged the Linq into ADO by using the method. net Command (SQL ).
For Linq, EF has defined some ANSI common query correspondence functions, such as the LIKE syntax Method for Contains correspondence.
var q = dbContext.Customers.Where(c => c.Name.Contains("XXX"));
However, EF does not have any fixed information on the design. Therefore, for some specific information, EF provides additional snapshot devices for processing.
For example, In the MS-SQL can pass through the SqlFunctions class to use the specific method in the MS-SQL, such
var q = dbContext.Customer.Where(c => SqlFunctions.DateDiff("ss", c.CreatedDate, DateTime.Today) > 0);
The above example goes through the DateDiff method in SqlFunctions to correspond to the DATEDIFF function in the SQL functions method. Of course, this method also indicates that the MS-SQL is optimized.
In addition, the User Define Function (User's self-defined Function number) Section EF can also provide a detailed exhibition, and the subsequent chapter will further explain this part.
Frequently Asked Questions or frequently asked questions when using Linq in EF.
For example, the following Code
var q = dbContext.Customers.Where(c => c.Name.Contants("XX") && Check(c));
The Check function is a C # program used by another author to Check whether the program meets the requirements.
From the above reading, we can know that when this method is used, there will be errors, because EntityClient Data Provider does not (or do not know ), how should the Check function be converted to ADO. net Command (SQL ).
So we have some confusions.
var q = dbContext.Customers.Where(c => c.Name.Contants("XX")).ToList().Where(c => Check(c));
Divide the limit method into two segments first. the ToList () method returns the data from the data source to the memory, because the data is already in the memory, therefore, the Where clause in the second paragraph is actually implemented through Linq to Object instead of EF.
Note that when operating EF, you must know when the information will be retrieved from the database.
Otherwise, if the preceding example is written as follows
var q = dbContext.Customers.ToList().Where( c.Name.Contants("XX") && c => Check(c));
Although the results are the same, the performance difference is quite high.
Because. when the ToList () method is used to parse rows, the MERs data in all the data records will be pulled back to the memory, and then the Where data row will be used in the program for the selection.
The preceding method goes through Where (c => c. name. contants ("xx") first uses the SQL statement method to insert the selected result to the memory and then perform subsequent queries.