The use of a legal LINQ query expression in a relational database is nothing more than a use of a legal LINQ query expression. In addition to the query itself, LINQ to SQL also provides many types in the system. Data. LINQ. dll assembly to facilitate data interaction between the code library and the physical database engine.
The main purpose of LINQ to SQL is to provide consistency between relational databases and their interactive programming logic. You can add, delete, modify, and query all expected databases and define the transaction context by using the LINQ query expression, the defined entity class, And the datacontext type, create a new database entity or the entire database, call the stored procedure, and other database-centric activities.
In addition, the types in LINQ to SQL can be integrated with standard ADO. NET data types. For example, an overloaded datacontext constructor accepts an object compatible with idbconnection as the input. In this way, the existing ADO. NET data access library can be integrated with the C #3.0 LINQ query expression, and vice versa. As a matter of fact, for Microsoft, LINQ to SQL is just a new member of the ADO. Net family.
Functions of entity classes
The first step you need to do when you apply the SQL statements to be written is to define the object class. In short, the entity class represents the relational data type you want to interact.
After defining the object class, you can use the datacontext type to upload the query expression to the relational database. This type translates your LINQ query expression into an appropriate SQL query statement and performs data exchange with a specific database. The datacontext type has many Members who map the query expression results to your defined object class.
The datacontext type also defines a factory mode for obtaining instances of entity classes that can be used in the code library. After obtaining an object instance, you can change its status and submit the modified object to datacontext for processing.
Assume that there is a car table in our database. The table structure is as follows:
Column Name Data Type
Id string primary key
Name string
Speed string
Vs defines an object class for the table:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data. LINQ. Mapping;
Using system. Data. LINQ;
Namespace linqtest
{
[Table]
Class car
{// Specify the primary key
[Column (isprimarykey = true)]
Public String ID;
[Column]
Public string name;
[Column]
Public int speed;
Public override string tostring ()
{
Return string. Format ("ID = {0}, name = {1}, speed = {2}", ID, name, Speed. tostring ());
}
}
}
Note: to define an object class, you must import the namespace system. Data. LINQ. Mapping and system. Data. LINQ.
The table feature is added to the object class, and each public field is marked with the column feature. In both cases, all names are directly mapped to the names of the corresponding physical data table. However, the tableattribute and columnattribute types both support the name attribute. In this way, we can split the data table's programming representation name from the physical data table name itself. Pay attention to the isprimarykey attribute of the columnattrimarytype. This attribute indicates that the modified field corresponds to the primary key of the data table.
For simplicity, each field is declared as public. Of course, we can also define private fields and encapsulate them with public attributes. In this case, we should use the column feature to mark these public attributes (rather than fields ).
An object class can contain any number of members that are not mapped to a data table. As for the time-to-time mechanism of LINQ, only those items marked with the LINQ to SQL feature will be used in data exchange.
With the entity class, we can use the datacontext type to submit our query expression to the specified database.
Sample Code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Data. LINQ;
Using system. text;
Namespace linqtest
{
Class Program
{
Static void main (string [] ARGs)
{
// Create a datacontext object
Datacontext DB = new datacontext (@ "Data Source =. \ sqlexpress; initial catalog = test; Integrated Security = true ");
// Get the car table data to the table <car> Object
Table <car> cartable = dB. gettable <car> ();
// Display all data in the car table
Foreach (VAR car in cartable)
{
Console. writeline (car. tostring ());
}
// Use LINQ to query records with speed> 10
Foreach (VAR car in from C in cartable where c. speed> 10 select C)
{
Console. writeline (car. tostring ());
}
// Use Lambda to query records with speed> 10
Foreach (VAR car in cartable. Where (CAR => Car. speed> 10 ))
{
Console. writeline (car. tostring ());
}
Console. readkey ();
}
}
}
In the preceding example, database queries are strongly typed, but there is still a gap between datacontext and its car entity classes. To solve this problem, a better solution is to create a class that extends the datacontext type and define member variables for each table it operates on.
Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace LinqTest
{
class TestDatabase : DataContext
{
public Table<Car> Car;
public TestDatabase(string connStr)
: base(connStr)
{
}
}
}
Now we can greatly simplify the code in the main method.
Sample Code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Data. LINQ;
Using system. text;
Namespace linqtest
{
Class Program
{
Static void main (string [] ARGs)
{
// Create a strongly typed datacontext object
Testdatabase DB = new testdatabase (@ "Data Source =. \ sqlexpress; initial catalog = test; Integrated Security = true ");
// Display all data in the car table
Foreach (VAR car in db. Car)
{
Console. writeline (car. tostring ());
}
// Use LINQ to query records with speed> 10
Foreach (VAR car in from C in dB. Car where c. speed> 10 select C)
{
Console. writeline (car. tostring ());
}
// Use Lambda to query records with speed> 10
Foreach (VAR car in db. Car. Where (CAR => Car. speed> 10 ))
{
Console. writeline (car. tostring ());
}
Console. readkey ();
}
}
}
What is the query expression of the LINQ to SQL statement?
The benefit of LINQ to SQL is that we can use a unified, Object-based model to interact with relational databases. So what is the LINQ query expression? Let's take a look at the output of the following code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Data. LINQ;
Using system. text;
Namespace linqtest
{
Class Program
{
Static void main (string [] ARGs)
{
// Create a strongly typed datacontext object
Testdatabase DB = new testdatabase (@ "Data Source =. \ sqlexpress; initial catalog = test; Integrated Security = true ");
// Define a query result set
VaR car = from C in dB. Car where c. speed> 10 select C;
// Check what is output here?
Console. writeline (car. tostring ());
Console. readkey ();
}
}
}
The output of this program is as follows:
You may be surprised that the query expression string shows the internal SQL query statement. Now you know what the query expression of LINQ to SQL is.
The content of LINQ to SQL is far more than that. We can use Visual Studio to automatically create a LINQ to SQL item by dragging the mouse, and its functions are amazing! The addition, deletion, modification, and query operations of all data tables are encapsulated. You only need to simply call them.