Summary

  • Language Integrated Query (LINQ) is a new technology that allows you to query data sources from within your code, without needing to rely on specific data translation classes.

  • LINQ uses the same syntax with traditional databases, but also other data sources such as XML files, and even with collections in your application.

  • LINQ allows you to issue queries against collections in your code, returning a subset that’s easier to work with.

  • A LINQ data source collection must implement IEnumerable, and the collection the query returns must as well.

  • The range variable is used to define the parameters of the query. It can be any valid C# name, and C# will automatically infer its type based on the returned values.

  • Once you have the enumerable collection of returned results, you can manipulate them as you would any generic collection.

  • You can use anonymous types and the var keyword in your queries to have the compiler infer the types in the query, whether simple or complex.

  • You can use lambda expressions in your queries to define a method for evaluating the stored data, and then project the results onto a variable.

  • The join clause can unite the data from two different collections, but it has a special syntax, requiring the name of the collection to join with, the field you’re joining on in each collection, and the fields that should be in the return set.

  • LINQ to SQL classes allows you to use the LINQ syntax to query a SQL database.

  • You need to include a reference to the System.Data.Linq namespace to use LINQ to SQL.

  • LINQ to SQL allows you to use attributes on classes so that they behave like tables, and on properties so that they behave like columns.

  • LINQ to SQL also features a DataContext class that can take a connection string in the constructor.

  • The Object Relational Designer adds a visual component to LINQ to SQL classes. You can drag-and-drop database tables directly onto the design surface, and the O/R Designer will supply appropriate backing code for the classes that represent the data.

  • You can use the automatically created classes as though they were ordinary classes in your code, simplifying database access.

LINQ is a remarkable new technology that has the potential to partially bridge the gap that’s existed between software development and database administration. Beyond that, you’ve seen how useful the LINQ syntax can be even without a database in play. With that said, however, LINQ is a very new technology, and it’s still evolving. It remains to be seen how well developers and database professionals will embrace it, or whether something else will come along. In that sense, unlike most of the rest of the book, this chapter represents something you’re not likely to see right away as you practice with C#, but we anticipate that LINQ will grow in popularity, and may become as much a part of the language as any of the other features you’ve seen so far.

That brings you to the end of the book, but not the end of learning C#. As we promised at the beginning, you started with the very fundamentals of the language and moved into intermediate topics like interfaces and delegates, and we presented a broad introduction to some more advanced topics in the last few chapters. As you’ve guessed, there’s a lot more to the language that we haven’t covered in this book, but now you have a foundation to build upon. There are many excellent books out there for going more deeply into C#, one of which is this book’s companion volume, Programming C# 3.0, by Jesse Liberty and Donald Xie (O’Reilly), where you’ll find additional detail and deeper discussion of the topics in this book. The best thing you can do to continue learning C#, however, is to practice writing code. The exercises you’ve done throughout this book are just a beginning; you can rewrite and extend them as much as you like. If you’re not sure about some syntax, or a language feature, try it out! If you’re wrong, most of the time the compiler will throw an exception, which can teach you as much as getting it right the first time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset