Chapter 15. Putting LINQ to Work

LINQ may be the most anticipated, most exciting (and to some, most feared) feature in C# 3.0. The previous two chapters were, in large measure, a necessary introduction, an appetizer to whet your appetite and get you ready for the main meal: using LINQ to retrieve meaningful data in production applications.

Before we begin, let's be clear: your DBA is terrified of LINQ, and not just as a matter of job security. Improperly used, LINQ has the ability to put queries into the hands of inexperienced, untrained goofballs (us) who know little or nothing about writing efficient queries, and who will bring carefully honed data-intensive enterprise systems to their knees (fun, eh?). OK, I said it out loud, so let's all stop panicking.

As with all programming, the trick is to write the program, get it working, and then optimize. It may be that after you have your program up and working (and profiled), you'll discover that there are some places that you've used LINQ that you'd be better off using stored procedures running within your database (that's what databases do for a living), but we don't know that a priori, and the advantages of LINQ are so tremendous (e.g., the ability to use an object-oriented unified syntax to access all your data regardless of source) that it cries out for a "code now, optimize later if needed" approach.

The two most common sources you'll use LINQ with are, no doubt, SQL and XML, but they are certainly not the only sources of data. ...

Get Programming C# 3.0, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.