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 SQL325us<strong>in</strong>g (var context = new DefectModelDataContext()){Defect d = (from defect <strong>in</strong> context.Defectswhere defect.ID==1select defect).S<strong>in</strong>gle();EF<strong>in</strong>ds defect withquery expression}Console.WriteL<strong>in</strong>e (d);List<strong>in</strong>g 12.5 is easy enough to follow—we open up a context and fetch the firstdefect B. After chang<strong>in</strong>g the defect and the entity represent<strong>in</strong>g Tim Trotter C, weask LINQ to SQL to save the changes to the database D. F<strong>in</strong>ally, we fetch thedefect E aga<strong>in</strong> <strong>in</strong> a new context and write the details to the console. Just for a bit ofvariety, I’ve shown two different ways of fetch<strong>in</strong>g the defect—they’re absolutelyequivalent, because the compiler translates the query expression form <strong>in</strong>to the“method call” form anyway.That’s all the LINQ to SQL we’re go<strong>in</strong>g to see—hopefully it’s shown you enough ofthe capabilities to understand how it’s a normal ORM system, but one that has goodsupport for query expressions and the LINQ standard query operators.12.1.5 LINQ to SQL summaryThere are lots of ORMs out there, and many of them allow you to build up queries programmatically<strong>in</strong> a way that can look like LINQ to SQL—if you ignore compile-timecheck<strong>in</strong>g. It’s the comb<strong>in</strong>ation of lambda expressions, expression trees, extensionmethods, and query expressions that make LINQ special, giv<strong>in</strong>g these advantages:■ We’ve been able to use familiar syntax to write the query (at least, familiar whenyou know LINQ to Objects!).■ The compiler has been able to do a lot of validation for us.■ Visual Studio 2008 is able to help us build the query with IntelliSense.■ If we need a mixture of client-side and server-side process<strong>in</strong>g, we can do both <strong>in</strong>a consistent manner.■ We’re still us<strong>in</strong>g the database to do the hard work.Of course, this comes at a cost. As with any ORM system, you want to keep an eye onwhat SQL queries are be<strong>in</strong>g executed for a particular query expression. That’s wherethe logg<strong>in</strong>g is <strong>in</strong>valuable—but don’t forget to turn it off for production! In particular,you will need to be careful of the <strong>in</strong>famous “N+1 selects” issue, where an <strong>in</strong>itial querypulls back results from a s<strong>in</strong>gle table, but us<strong>in</strong>g each result transparently executesanother query to lazily load associated entities. Sometimes you’ll be able to f<strong>in</strong>d anelegant query expression that results <strong>in</strong> exactly the SQL you want to use; other timesyou’ll need to bend the query expression out of shape somewhat. Occasionally you’llneed to write the SQL manually or use a stored procedure <strong>in</strong>stead—as is often the casewith ORMs.I f<strong>in</strong>d it <strong>in</strong>terest<strong>in</strong>g just to take query expressions that you already know work <strong>in</strong>LINQ to Objects and see what SQL is generated when you run them aga<strong>in</strong>st a database.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!