13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

LINQ to SQL315LINQ to DataSet and LINQ to XML are both frameworks that tackle exist<strong>in</strong>g problems(manipulat<strong>in</strong>g datasets and XML respectively) but do so <strong>in</strong> a LINQ-friendly fashion.Both of them use LINQ to Objects for the underly<strong>in</strong>g query support, but the APIshave been designed so that query expressions can be used to access data <strong>in</strong> a pa<strong>in</strong>lessand consistent manner.Hav<strong>in</strong>g covered the LINQ APIs shipped with .NET 3.5, we’ll take a peek at someother providers. Microsoft developers aren’t the only ones writ<strong>in</strong>g LINQ providers,and I’ll show a few third-party examples, before reveal<strong>in</strong>g what Microsoft has <strong>in</strong> storefor us with the ADO.NET Entity Framework and Parallel LINQ.This chapter is not meant to provide you with a comprehensive knowledge of us<strong>in</strong>gLINQ by any means: it’s a truly massive topic, and I’m only go<strong>in</strong>g to scratch the surfaceof each provider here. The purpose of the chapter is to give you a broad idea of whatLINQ is capable of, and how much easier it can make development. Hopefully there’llbe enough of a “wow” factor that you’ll want to study some or all of the providers further.To this end, there are great onl<strong>in</strong>e resources, particularly blog posts from the variousLINQ teams (see this book’s website for a list of l<strong>in</strong>ks), but I also thoroughlyrecommend LINQ <strong>in</strong> Action (Mann<strong>in</strong>g 2008).As well as understand<strong>in</strong>g LINQ itself, by the end of this chapter you should see howthe different pieces of the <strong>C#</strong> 3 feature set all fit together, and why they’re all present<strong>in</strong> the first place. Just as a rem<strong>in</strong>der, you shouldn’t expect to see any new features of <strong>C#</strong>at this po<strong>in</strong>t—we’ve covered them all <strong>in</strong> the previous chapters—but they may wellmake more sense when you see how they help to provide unified query<strong>in</strong>g over multipledata sources.The change <strong>in</strong> pace, from the detail of the previous chapters to the spr<strong>in</strong>t throughfeatures <strong>in</strong> this one, may be slightly alarm<strong>in</strong>g at first. Just relax and enjoy the ride,remember<strong>in</strong>g that the big picture is the important th<strong>in</strong>g here. There won’t be a testafterward, I promise. There’s a lot to cover, so let’s get crack<strong>in</strong>g with the most impressiveLINQ provider <strong>in</strong> .NET 3.5: LINQ to SQL.12.1 LINQ to SQLI’m sure by now you’ve absorbed the message that LINQ to SQL converts query expressions<strong>in</strong>to SQL, which is then executed on the database. There’s more to it than that,however—it’s a full ORM solution. In this section we’ll move our defect system <strong>in</strong>to aSQL Server 2005 database, populate it with the sample defects, query it, and update it.We won’t look at the details of how the queries are converted <strong>in</strong>to SQL until the nextsection, though: it’s easier to understand the mechanics once you’ve seen the endresult. Let’s start off by gett<strong>in</strong>g our database and entities up and runn<strong>in</strong>g.12.1.1 Creat<strong>in</strong>g the defect database and entitiesTo use LINQ to SQL, you need a database (obviously) and some classes represent<strong>in</strong>gthe entities. The classes have metadata associated with them to tell LINQ to SQL howthey map to database tables. This metadata can be built directly <strong>in</strong>to the classes us<strong>in</strong>gLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!