tailieunhanh - ASP.NET 4 Unleased - p 100

Thực hiện các lệnh với tiêu chuẩn cơ sở dữ liệu LINQ to SQL Trong phần này, bạn học cách sử dụng LINQ to SQL như là thay thế một để làm việc trực tiếp với SQL. Chúng tôi bắt đầu bằng việc thảo luận làm thế nào LINQ to SQL truy vấn khác với tiêu chuẩn truy vấn LINQ. | 964 CHAPTER 20 Data Access with LINQ to SQL Performing Standard Database Commands with LINQ to SQL In this section you learn how to use LINQ to SQL as a replacement for working directly with SQL. We start by discussing how LINQ to SQL queries differ from standard LINQ queries. Next we examine how you can perform standard database queries and commands using LINQ to SQL such as Select Update Insert and Delete commands. We ll also discuss how you can create dynamic queries with LINQ. Finally we investigate the important topic of how you can debug LINQ to SQL queries. LINQ to Objects Versus LINQ to SQL You can use standard LINQ LINQ to Objects with any object that implements the IEnumerable T interface interface . You can use LINQ to SQL on the other hand with any object that implements the IQueryable T interface. Standard LINQ is implemented with the extension methods exposed by the class. LINQ to SQL on the other hand uses the extension methods exposed by the class. Why the difference When you build a query using standard LINQ the query executes immediately. When you build a query using LINQ to SQL on the hand the query does not execute until you start enumerating the results. In other words the query doesn t execute until you use a foreach loop to walk through the query results. Consider the following valid LINQ to SQL query var query m Steven Spielberg .OrderBy m .Select m This query returns a list of movies directed by Steven Spielberg in order of the movie box office totals. You want LINQ to SQL to execute this query against the database in the most efficient way possible. In particular you don t want LINQ to SQL to execute each method independently you want LINQ to SQL to execute one smart database query. When executing this query it would be bad if LINQ to SQL 1 grabbed all the Movie records that were directed by Steven Spielberg 2 sorted the records and then 3 .