14.5. Using LINQ
SQL is so simple to use, wouldn't it be nice if we could apply its simplicity to data structures other than databases? That's what Microsoft's latest creation, LINQ, is designed to do. LINQ stands for Language INtegrated Query and is a new feature with Visual Studio 2008. While SQL may be used with relational database objects, LINQ is also capable of querying object types where the data source is not a database. You can use LINQ to query any object type, including arrays, class objects, and XML, in addition to relational databases. Visual Studio incorporates the LINQ query engine directly, but also has defined an extension definition that allows third-party data sources to tie into the engine via a translator. Just as SQL queries result in datasets that are stored in memory, LINQ returns a collection of memory-based objects.
14.5.1. LINQ Query Keywords
SQL has specific keywords that are used in query statements, and LINQ provides a similar set of keywords. Perhaps the easiest way to begin explaining LINQ is to use a simple example.
Try It Out: A LINQ Program ExampleIn this example, the program generates MAXNUM (such as 100) random numbers and displays them in a listbox object. The program then uses LINQ to query that data for values that fall within a specified range. A sample run of the program can be seen in Figure 14-16. Figure 14-16. Figure 14-16The specified ... |
Get Beginning C# 3.0 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.