Databases Reference
In-Depth Information
Now you are ready to work with a LINQ project, and all you need to do is add the
code functionality and required namespaces to the project and test the application. Let's
begin using LINQ.
Using LINQ to Objects
The term LINQ to Objects refers to the use of LINQ queries to access in-memory data
structures. You can query any type that supports IEnumerable<T> . This means that you can
use LINQ queries not only with user-defined lists, arrays, dictionaries, and so on, but also
in conjunction with .NET Framework APIs that return collections. For example, you can
use the System.Reflection classes to return information about types stored in a specified
assembly, and then filter those results by using LINQ. Or you can import text files into
enumerable data structures and compare the contents to other files, extract lines or parts
of lines, group matching lines from several files into a new collection, and so on.
LINQ queries offer three main advantages over traditional foreach loops:
• They are more concise and readable, especially when filtering multiple conditions.
• They provide powerful filtering, ordering, and grouping capabilities with a mini-
mum of application code.
• They can be ported to other data sources with little or no modification.
In general, the more complex the operation you want to perform on the data, the
greater the benefit you will realize by using LINQ as opposed to traditional iteration
techniques.
Try It Out: Coding a Simple LINQ to Objects Query
In this exercise, you'll use LINQ to Objects to retrieve some names from an array of
strings.
1. Right-click the Chapter19 project in the Chapter19 solution, select the Rename
option, and rename the project to LinqToObjects. Rename Program.cs to
LinqToObjects.cs . Replace the code in LinqToObjects.cs with the code in
Listing 19-1.
Search WWH ::




Custom Search