13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LINQ to DataSet335where defect.Field("Status") != Status.Closedselect defect.Field("Summary");foreach (str<strong>in</strong>g summary <strong>in</strong> query){Console.WriteL<strong>in</strong>e (summary);}SelectsSummary fieldCom<strong>in</strong>g so soon after the nice, clean world of LINQ to SQL, list<strong>in</strong>g 12.10 makes mefeel somewhat dirty. There’s hard-coded SQL, column names, and casts all over theplace. However, we’ll see that th<strong>in</strong>gs are better when we have a typed dataset—and thiscode does get the job done. If you’re us<strong>in</strong>g both LINQ to SQL and LINQ to DataSet, youcan fill a DataTable us<strong>in</strong>g the DataTableExtensions.CopyToDataTable extensionmethod, but I wanted to keep to just one new technology at a time for this example.The first part B is “old-fashioned” ADO.NET code to fill the data table. I haven’tused an actual dataset for this example because we’re only <strong>in</strong>terested <strong>in</strong> a s<strong>in</strong>gletable—putt<strong>in</strong>g it <strong>in</strong> a dataset would have made th<strong>in</strong>gs slightly more complicated forno benefit. It’s only when we reach the query expression (C D and E) that LINQstarts com<strong>in</strong>g <strong>in</strong>.The source of a query expression has to be enumerable <strong>in</strong> some form—and theDataTable type doesn’t even implement IEnumerable, let alone IEnumerable. TheDataTableExtensions class provides the AsEnumerable extension method (C), whichmerely returns an IEnumerable that iterates over the rows <strong>in</strong> the table.Access<strong>in</strong>g fields with<strong>in</strong> a row is made slightly easier <strong>in</strong> LINQ to DataSet us<strong>in</strong>g theField extension method on DataRow. This not only removes the need to castresults, but it also deals with null values for you—it converts DBNull to a null referencefor you, or the null value of a nullable type.I won’t give any further examples of untyped datasets here, although there are acouple more queries <strong>in</strong> the book’s sample code. Hopefully you’ll f<strong>in</strong>d yourself <strong>in</strong> thesituation where you can use a typed dataset <strong>in</strong>stead.12.3.2 Work<strong>in</strong>g with typed datasetsAlthough typed datasets aren’t as rich as us<strong>in</strong>g LINQ to SQL directly, they providemuch more static type <strong>in</strong>formation, which lets your code stay cleaner. There’s a bit ofwork to start with: we have to create a typed dataset for our defect-track<strong>in</strong>g systembefore we can beg<strong>in</strong> us<strong>in</strong>g it.CREATING THE TYPED DATASET WITH VISUAL STUDIOThe process for generat<strong>in</strong>g a typed dataset <strong>in</strong> Visual Studio 2008 is almost exactly thesame as it is to generate LINQ to SQL entities. Aga<strong>in</strong>, you add a new item to the project(this time select<strong>in</strong>g DataSet <strong>in</strong> the list of options), and aga<strong>in</strong> you can drag and droptables from the Server Explorer w<strong>in</strong>dow onto the designer surface.There aren’t quite as many options available <strong>in</strong> the property panes for typeddatasets, but we can still rename the DefectUser table, the DefectID field, along withthe associations. Likewise, we can still tell the Status, Summary, and UserType propertiesto use the enumeration types from the model. Figure 12.4 shows the designerafter a bit of edit<strong>in</strong>g and rearrang<strong>in</strong>g.EAccessesStatusfieldDLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!