Visual studio is able to generate derived classes to

Info icon This preview shows pages 567–569. Sign up to view the full content.

View Full Document Right Arrow Icon
Visual Studio is able to generate derived classes to build a so-called strongly typed DataSet , whose tables offer row objects with .NET properties representing columns in the corresponding database table. Strongly typed DataSet s are often used to reduce the amount of code required to bridge between C# and the database. However, since LINQ to SQL and LINQ to Entities came along, this use of DataSet s has become less popular, The .NET Data Access Landscape | 543
Image of page 567

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
because the LINQ-based approaches offer the same benefit but are typically easier to use. So DataSet s are somewhat out of favor today. The low-level ADO.NET data access interfaces were the main way to access data in .NET right up until .NET 3.5 and Visual Studio 2008 shipped, bringing LINQ. LINQ and Databases As we saw in Chapter 8 , LINQ lets you perform tasks with collections of data including filtering, sorting, and grouping. In that chapter, we were working only with objects, but these are exactly the jobs that databases are good at. Moreover, one of the moti- vations behind LINQ’s design was to make it easier to use databases from code. As you can see in Example 14-2 , LINQ blends data access seamlessly into C# code—this da- tabase example looks very similar to the object examples we saw in the earlier chapter. Database LINQ Providers Database LINQ providers work very differently from the LINQ to Objects provider, even though queries use the same syntax for both. In LINQ to Objects, a where clause does all of its work inside the .NET Framework—it’s similar in action to a loop with an if statement. But trying that with a database would be a disaster—if your where clause is designed to select a single row out of 20 million, you absolutely don’t want C# code to iterate through all 20 million rows! You want the database to do the filtering so that it can use its indexes to locate the row efficiently. And it works exactly as you’d want—the LINQ where clause in Example 14-2 is effec- tively translated into a SQL WHERE clause. As you may recall, C# converts a LINQ query expression into a series of method calls, and those method calls just end up building a query object that knows how to return the results. LINQ uses deferred execution —the query doesn’t start returning results until you ask for them. LINQ providers for data- bases do something similar, but instead of working directly with IEnumerable<T> , they use a specialized type that derives from IEnumerable<T> , called IQueryable<T> . Since IQueryable<T> derives from IEnumerable<T> , you can still enumerate its contents in the usual ways, but it’s only when you do this that it generates a suitable database query; it won’t touch the database until you start asking to see elements. So we still have deferred execution, but crucially, when you finally execute the query the complete chain of processing that your LINQ query represents is turned into a single SQL query so that the database can do all the work.
Image of page 568
Image of page 569
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern