Information or data processing is the most important task of any software and one of the main obstacles facing current developers is the difference between object-oriented programming language and language. Data query, the problem is more complex with the emergence of XML ( eXtensible Markup Language ).
Currently, the most common way to get data from database systems (SQL databases) is to use SQL ( Structure Query Language ). SQL has a very different syntax from popular programming languages such as C # and VB.NET, so programmers have to work hard to "mend" these two different entities together in each software project.
Another problem with SQL is that it is only used to query data in relational databases. If you want to access XML data or other forms (such as HTML pages, email .), the developer must use another query syntax (XPath / XQuery).
To reduce the burden of working on many different languages and improve programming productivity, Microsoft developed a data integration solution for the .NET Framework called LINQ (Language Integrated Query), which is an open library. for C # and Visual Basic .NET programming languages (which can be extended for other languages) provide the ability to directly query object data, databases, and XML. (Figure 1)
Query object data in memory
Data needs to be dumped into memory to process, but once separated from its original location, the query capability is very poor. You can easily query customer information associated with their order information from the SQL Server database but it is not easy to do the same with the information in memory. In a .NET environment, information (in memory) is often expressed in the form of objects and before LINQ, there is no way to concatenate objects or perform any query operations. LINQ is the solution to this problem.
For example, in SQL Server, we can query all records (records or rows) from the Customer table in the following way:
SELECT * FROM Customer
The return value is a "result set" that is similar to a data table, containing all the fields (fields) of the Customer table.
Using LINQ, we can do the same query with the C # or VB.NET commands, but only query the list of objects in memory instead of the table in the database. The following simple example uses "data source" as a string array, in VB.NET:
LINQ clears query barriers Picture 1 Dim names As String () = {"Long", "Lan", "Qui", "Phung"}
Objects in the names array are named name.
ForEach name As String in names
'name.xxx
EndFor
Using LINQ syntax, we can query this "data source" similar to table queries with SQL.
Chọn tên từ tên trong tên
List the object (array) names here equivalent to the Customer table in the above SQL statement.
Because .NET is an object environment, everything is based on objects, attributes, and methods. So both the data source that we query as well as the returned result set are objects. So we need to declare the variable for the Select statement (or the result of the Select statement), for example:
Dim result As IEnumerable (Of String) = Select name From name in names
Similarly, in C #:
IEnumerable result = from name in names select names;
LINQ has enough query operators on object data similar to SQL on databases, such as order (order), condition (where) or join .
The feature to query objects in memory opens many interesting possibilities. For example, you can query all textboxes in a certain valid form, and link them to objects of a "union" with the query result set from the database or resource. XML data.
Querying "real" databases
Of course, data is not only in memory. There are two other important places that often contain data such as database systems (SQL Server) and XML documents (these "real" data are physically stored, have longer "live" time than "virtual" data in the set). mind). LINQ has two sets of API functions used to query these data sources: DLINQ uses query relational databases (SQL) and XLINQ uses hierarchical data queries (XML).
DLINQ
DLINQ is a special set of classes that allows displaying tables and data rows in the form of objects, so that LINQ can be used to query the database directly. (Figure 2)
DLINQ uses the DataContext object to open a connection to the database. Then use Table <> class to represent the data table, and with this object, we can use LINQ syntax to query.
The following example queries all customers with the company name starting with the "T" from the Northwind database of SQL Server, using the C # command syntax:
DataContext context =
new DataContext ("Initial Catalog = Northwind;" +
"Integrated Security = sspi");
Table customers =
context.GetTable ();
var result =
from c in customers
where c.CompanyName.StartsWith ("T")
select c;
XLINQ
What DLINQ does with databases, XLINQ does with XML.
Consider the following XML string:
PC World Vietnam
The Gioi Vi Tinh
.
XLINQ allows putting this XML string into XElement object to query with LINQ syntax.
XElement names = XElement.Parse (xmlString);
var result =
from n in names.Descendants ("customer")
where n.Descendants ("companyName")
.Value.StartsWith ("T")
select n.Descendants ("contactName"). Value;
This query returns a list of strings containing the contact name of all customers whose company name begins with the letter "T".
XLINQ also has other attractive features: XML creation. This is also done with XElement and other XLINQ objects.
LINQ clears query barriers Picture 2 For example, consider the following XML data:
Kiểm TRA
We can create the above XML data with XLINQ objects.
XElement xml = new XElement ("root",
new XElement ("sub", "Test");
Console.Write (xml.ToString ());
More information about LINQ
The LINQ project has been pursued by Microsoft for many years and was officially announced at the Professional Developer Conference - PDC 2005 (September). Next, in May 2006, Microsoft released a trial LINQ library (http://msdn.microsoft.com/data/ref/linq/) that can be installed with Visual Studio 2005. Expected, LINQ will be integrated. in the next version of Visual Studio codenamed "Orcas" ( Object-Relational Component Architecture ).
One of the key players in the LINQ project is the chief architect of the C # language and the author of Turbo Pascal and Delphi (language with strong data processing capabilities): Anders Hejlsberg (refer joined Microsoft since 1996).