Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Professional ASP.NET 3.5 in C# and Visual Basic Part 54. Building on the revolutionary ASP.NET 2.0 release, ASP.NET 3.5 adds several key new developer features including AJAX, LINQ, and a new CSS designer in Visual Web Developer 2008. The dramatic reduction in code that developers realized from the more than 50 new server controls in ASP.NET 2.0 now allows developers the time to make their applications more interactive with AJAX, to work with data in their preferred language with LINQ, and to build visually attractive and consistent standards-based sites with CSS. . | Chapter 9 Querying with LINQ Me.GridView1.DataBind End Sub C protected void Page_Load object sender EventArgs e MoviesDataContext dc new MoviesDataContext var query from m in dc.Movies select m System.Diagnostics.Debug.WriteLine query this.GridViewl.DataSource query this.GridView1.DataBind Now when you debug the Web site using Visual Studio you can see the SQL query as shown in Figure 9-11. Output H SELECT tO . Title r tO . Director r tO . Genre r tO . Runtime r tO . ReleaseDate FROM dbo . Movies AS tO I i j Call Stack Breakpoints 7 Command Window f Immediate Windowj J Output Pending Checkins Figure 9-11 The SQL query generated by LINQ to SQL As you can see the SQL generated is standard SQL syntax and LINQ is quite good at optimizing the queries it generates even for more complex queries such as the grouping query shown in Listing 9-26. Listing 9-26 Grouping LINQ to SQL data VB Protected Sub Page_Load ByVal sender As Object ByVal e As System.EventArgs Dim dc As New MoviesDataContext Dim query From m In dc.Movies _ Group By m.Genre Into g Group Count System.Diagnostics.Debug.WriteLine query Me.GridViewl.DataSource query 486 Chapter 9 Querying with LINQ Me.GridView1.DataBind End Sub C protected void Page_Load object sender EventArgs e MoviesDataContext dc new MoviesDataContext var query from m in dc.Movies group m by m.Genre into g select new Genre g.Key Count g.Count System.Diagnostics.Debug.WriteLine query this.GridViewl.DataSource query this.GridView1.DataBind The generated SQL for this query is shown in Figure 9-12. My Favorite Movies - Windo. 1 iQl - gj http lo alhostS86 X My Favorite Movies Count Genre 10 0 21 1 3 2 Ml Local intranet Protected M 100 Figure 9-12 Grouping SQL data using LINQ to SQL Note that SQL to LINQ generates SQL that is optimized for SQL Server 2005. LINQ also includes a logging option you can enable by setting the Log property of the data context. While LINQ to SQL does an excellent job generating the SQL query syntax there may be times .