Introduction to Query expressions (LINQ)
In the last two articles, I introduced the features and improvements of C #3.0 new language.ProgramIt provides us with great help. From the beginning of this article, we will start to discuss the LINQ.
LINQ is short for Language Integrated Query, which is integrated in. net.Programming Language. It has become an integral part of programming languages. During programming, you can get good syntax check, rich metadata, and advantages of smart sensing, static type, and other strong types of languages. In addition, it also enables queries to conveniently Query Information in the memory, not just external data sources.
LINQ defines a set of standard query operators for all. in the programming language of the. NET platform, you can directly declare a unified method for cross-, filtering, and projection operations. The standard query operator allows the query to act on all sources based on ienumerable <t> interfaces, it also allows third-party specific domain operators suitable for the target domain or technology to expand the standard query operator set. More importantly, third-party operators can use their own implementation of additional services to freely replace standard query operators. According to the Convention of the LINQ mode, these queries prefer the same language integration and tool support as standard query operators.
Let's take a look at the overall LINQ architecture.
In. net3.5, Microsoft provides us with some namespaces.
There are five parts of LINQ to objects, LINQ to datasets, LINQ to SQL, LINQ to entities, and LINQ to XML.
A. Net Language Integrated Query Based on relational data. It is used to manage relational data in the form of objects and provides rich query functions. It is built on the integration of SQL-Based Schema definitions in public language systems. When maintaining the relational model expression capability and the performance of direct query and Evaluation of underlying storage, this integration provides strong types of relational data.
In the system. xml. LINQ namespace, implement the XML operations. Uses efficient, easy-to-use, in-memory XML tools to provide XPath/XQuery functions in the host programming language.
After talking about this, we still use a simple example to describe the experience brought by LINQ.
Step 1: Create a database mark language (dbml. Database description language, a document in XML format used to describe the database) file. Taking the northwind database as an example, the preceding MERs class is mapped into a table that corresponds to the customers table in the database.
Step 2: Create an ASP. NET page and add a gridview control to the page
Step 3: CompileCodeBind data
Step 4: run the displayed result.
Well, let's talk about that. You should have a general understanding of LINQ. Finally, I still have some questions about LINQ. I would like to ask a friend who is familiar with it to discuss it:
- LINQ is based on ADO. net. Will it replace ADO. net in the future?
- What is the efficiency of using LINQ in large projects?
Next, let's start with the LINQ to SQL statement to get a full understanding of the LINQ. Let's start with the simplest where Statement, which is also the most commonly used in programming.
Where operation
Applicable scenarios:Filters and queries.
Note:Similar to the WHERE clause in SQL commands, it is used to define the scope, that is, filter, and the judgment condition is the clause followed by it.
The where operation includes three forms: simple form, relational condition form, and first () form. The following example uses an instance:
1. Simple Form:
VaRQ =FromCInDB. MERsWhereC. City ="London"SelectC;
VaRQ =FromEInDB. EmployeesWhereE. hiredate> =NewDatetime(1994, 1, 1)SelectE;
2. Link Condition form:
VaRQ =FromPInDB. ProductsWhereP. unitsinstock <= P. reorderlevel &&! P. discontinuedSelectP;
VaRQ =FromPInDB. ProductsWhereP. unitprice> 10 M | P. discontinuedSelectP;
VaRQ = dB. Products. Where (P => P. unitprice> 10 m). Where (P => P. discontinued );
3. First () format:
Returns an element in the set. The essence is to add top (1) to the SQL statement)
ShipperShipper = dB. Shippers. First ();
CustomerCust = dB. Customers. First (C => C. customerid ="Bonap");
OrderOrd = dB. Orders. First (O => O. Freight> 10.00 m );