Microsoft free book "Introducing Microsoft LINQ" Translation-LINQ Overview

Source: Internet
Author: User

This book is intended for personal learning and knowledge sharing. The copyright of this book is owned by the original author. If there is any infringement, please inform me that I will handle the post immediately.
Reprinting is allowed, but this copyright statement is not allowed for commercial purposes!
Blog: Han xilong

Chapter 1LINQIntroduction

Overview

Through surfing the Internet, you can find instructions on language integration query (LINQ), such as the following:

· LINQ is a unified programming model for any type of data. You can use a data source-independent model to query and manipulate data.

· LINQ is just another tool that embeds SQL queries into code.

· LINQ is another data abstraction layer.

All of these descriptions are true to a certain extent, but they only mention a single aspect. LINQ can do a lot of things, not just embedded SQL queries. It is easier to use than the "unified programming model" and far more than just another set of rules for data modeling.

LINQ is a technology that simplifies and implements any data access interface. LINQ does not force you to use a specific architecture, which facilitates access to data from existing architectures. Like every tool, it may be used well or badly. To make full use of LINQ, you must be proficient in it.

Today, program-controlled data can belong to different data domains: An array, an object graph, an XML file, and a database ), A text file, a registry key, an e-mail message, and Simple Object Access Protocol, a Microsoft Excel table ...... Not to mention.

Each data domain has its own access method. When querying a database, you naturally use SQL. When you access XML data, you naturally use Dom or XQuery. To locate an object graph, you traverse an array and construct your own algorithms. You use a specific application (APIS) to access other data domains, such as Office EXCEL tables, an email, or other Microsoft Windows registries. The final result is that different programming models are created when different data sources are accessed.

There are already several ways to integrate multiple data access technologies into a comprehensive model. For example, the ODBC provider allows you to query Excel files like WMI stores data. However, in this way, you can use statements like SQL to access data expressed through the relational model. Sometimes data in a hierarchy or graphic model is obviously more efficient than relational data. Moreover, if the data model has nothing to do with the language, you may have to manage different types of systems. All of this results in an "impedance mismatch" between the data and the code )". To solve these problems, LINQ provides a unified way to access and operate data, without forcing a "one size fits all" model. Operations between these data models (Operations)Instead of balancing different architectures (Structures).

In this chapter, we will give a comprehensive introduction to LINQ. In the rest of this book, you will be able to deepen your understanding of this new and powerful technology.

  What is LINQ?

LINQ is a programming model that introduces query as the first class to any language in Microsoft. NET. However, in the language in use, full support for LINQ requires some extensions that greatly increase productivity and therefore provide a shorter, meaningful, and expressive syntax to manipulate data.

 

The following is a simple typical software solution for the LINQ query, which returns a collection of names of all clients in Italy in the country:

var query =
    from   c in Customers
    where  c.Country == "Italy"
    select c.CompanyName;

 

Do not worry about syntax and keywords (suchVaR keyword). The query result is a strings set. In C #, you can use a foreach statement to enumerate the result:

foreach ( string name in query ) {
    Console.WriteLine( name );
}

 

The two query definitions and foreach loops are common in C #3.0 statements. Here, you may wonder what we are querying.MERsWhat is it? Is this a new type of Embedded SQL query? No. The same query (the followingForeachCode) for a SQL database, a dataset, an array in the memory, or other types of data.MERsIs a collection of objects:

Customer[] Customers;

 

MERsYou can alsoDatasetOneDatatable:

DataSet ds = GetDataSet();
DataTable Customers = ds.Tables["Customers"];

 

MERsIt can also be an entity class that describes a physical table in a relational database:

DataContext db = new DataContext( ConnectionString );
Table<Customer> Customers = db.GetTable<Customer>();

 

Finally,MERsIt can also be an entity class that describes the conceptual model and maps to a relational database:

NorthwindModel dataModel = new NorthwindModel();
ObjectQuery<Customer> Customers = dataModel.Customers;

 

As you can see, the SQL-like syntax used in LINQ is called a query expression (Query expression). The language that implements embedded SQL only defines simple syntax to embed SQL statements into different languages, but these statements are not integrated into the syntax and type systems of these languages. For example, you cannot call a function written in the main language in the middle of the SQL statement, although it is possible in LINQ. LINQ not only queries databases, but also embedded SQL statements.

:

This topic lexicon:

[1] data abstraction layer: data abstraction action Layer

[2] unified programming model: uniform programming model

[3] object graph

[4] Simple Object Access Protocol: Simple Object Access Protocol

[5] relational model: Relational Model

[6] impedance detuning: impedance mistach

[7] integration: One size fits all

[8] query expression: query expression

 

Previous Article: introducing Microsoft LINQ-copyright notice and book Introduction

Next article: Working Principle of LINQ, relational model, XML operation

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.