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.

334 CHAPTER 12 LINQ beyond collections12.3 LINQ to DataSetSee<strong>in</strong>g all the neat stuff that LINQ to SQL can achieve is all very well, but most developersare likely to be improv<strong>in</strong>g an exist<strong>in</strong>g application rather than creat<strong>in</strong>g a new onefrom the ground up. Rather than ripp<strong>in</strong>g out the entire persistence layer and replac<strong>in</strong>git with LINQ to SQL, it would be nice to be able to ga<strong>in</strong> some of the advantages ofLINQ while us<strong>in</strong>g exist<strong>in</strong>g technology. Many ADO.NET applications use datasets,whether typed or untyped 4 —and 4 LINQ to DataSet gives you access to a lot of the benefitsof LINQ with little change to your current code.The query expressions used with<strong>in</strong> LINQ to DataSet are just LINQ to Objects queries—there’sno translation <strong>in</strong>to a call to DataTable.Select, for example. Instead,data rows are filtered and ordered with normal delegate <strong>in</strong>stances that operate onthose rows.Unsurpris<strong>in</strong>gly, you’ll get a better experience us<strong>in</strong>g typed datasets, but a set ofextension methods on DataTable and DataRow make it at least possible to work withuntyped datasets too. In this section we’ll look at both k<strong>in</strong>ds of datasets, start<strong>in</strong>g withuntyped ones.12.3.1 Work<strong>in</strong>g with untyped datasetsUntyped datasets have two problems as far as LINQ is concerned. First, we don’t haveaccess to the fields with<strong>in</strong> the tables as typed properties; second, the tables themselvesaren’t enumerable. To some extent both are merely a matter of convenience—wecould use direct casts <strong>in</strong> all the queries, handle DBNull explicitly and so forth, as well asenumerate the rows <strong>in</strong> a table us<strong>in</strong>g dataTable.Rows.Cast. Theseworkarounds are quite ugly, which is why the DataTableExtensions and DataRow-Extensions classes exist.Code us<strong>in</strong>g untyped datasets is never go<strong>in</strong>g to be pretty, but us<strong>in</strong>g LINQ is far nicerthan filter<strong>in</strong>g and sort<strong>in</strong>g us<strong>in</strong>g DataTable.Select. No more escap<strong>in</strong>g, worry<strong>in</strong>gabout date and time formatt<strong>in</strong>g, and similar nast<strong>in</strong>ess.List<strong>in</strong>g 12.10 gives a simple example. It just fills a s<strong>in</strong>gle defect table and pr<strong>in</strong>ts thesummaries of all the defects that don’t have a status of “closed.”List<strong>in</strong>g 12.10Display<strong>in</strong>g the summaries of open defects from an untyped DataTableDataTable dataTable = new DataTable();us<strong>in</strong>g (var connection = new SqlConnection(Sett<strong>in</strong>gs.Default.SkeetySoftDefectsConnectionStr<strong>in</strong>g)){str<strong>in</strong>g sql = "SELECT Summary, Status FROM Defect";new SqlDataAdapter(sql, connection).Fill(dataTable);}var query = from defect <strong>in</strong> dataTable.AsEnumerable()BCFills tablefromdatabaseMakes tableenumerable4An untyped dataset is one that has no static <strong>in</strong>formation about the contents of its tables. Typed datasets, usuallygenerated <strong>in</strong> the Visual Studio designer, know the tables which can be present <strong>in</strong> the dataset, and the columnswith<strong>in</strong> the rows <strong>in</strong> those tables.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!