15.1 Understanding the Role of LINQ 15.2 A First Look at LINQ Query Expressions 15.3 LINQ and Generic Collections 15.4 LINQ and Nongeneric Collections 15.5 The Internal Representation of
Trang 2“LINQ allows you to build strongly typed query expressions, which can be applied to a number of LINQ targets to manipulate “data” in the broadest sense of the word Here, you will learn about LINQ to Objects, which allows you to apply LINQ expressions to containers of data (arrays, collections, custom types) This information will serve you well when we examine how to apply LINQ expressions to relational databases (via LINQ to ADO) and XML documents.”
Trang 315.1 Understanding the Role of LINQ
15.2 A First Look at LINQ Query Expressions
15.3 LINQ and Generic Collections
15.4 LINQ and Nongeneric Collections
15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators
Trang 415.1 The Role of LINQ
The LINQ API is an attempt to provide a consistent,
symmetrical manner in which programmers can obtain and manipulate “data” in the broad sense of the term
Trang 5The Role of LINQ
Trang 615.1 Understanding the Role of LINQ
15.2 A First Look at LINQ Query Expressions
15.3 LINQ and Generic Collections
15.4 LINQ and Nongeneric Collections
15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators
Trang 715.2 A First Look at LINQ Query
Expressions
LINQ and Implicitly Typed Local Variables
Trang 8LINQ Query Expressions
and can’t subtype?
IEnumerable<T>, without deriving a new interface?
type, but the syntax makes them look that way
Trang 9LINQ Query Expressions
Trang 10LINQ Query Expressions
T
parameter tells CLR the method is an Extension
the object
Trang 11Replacing LINQ Extension Methods
members
extension method, the type’s member will be called, and there is
no ambiguity
implementations of the Standard Query Operators
Trang 13 Lambda expressions
Lambda Expression
Trang 14Lambda Expression
Trang 15The Role of Differed Execution
LINQ query expressions are not actually evaluated until you iterate over their contents
Apply the same LINQ query multiple times to the same container and obtain the latest and greatest results
Trang 16The Role of Immediate Execution
To evaluate a LINQ expression from outside the confines
of foreach logic, you are able to call any number of
extension methods defined by the Enumerable type
Extension methods:ToArray<T>,
ToDictionary<TSource,TKey>(), and ToList<T>()
Capture a LINQ query result set in a strongly typed
container
Trang 1715.1 Understanding the Role of LINQ
15.2 A First Look at LINQ Query Expressions
15.3 LINQ and Generic Collections
15.4 LINQ and Nongeneric Collections
15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators
Trang 1815.3 LINQ and Generic Collections
LINQ query expressions can also manipulate data within members of the System.Collections.Generic namespace, such as the List<T> type
Trang 20Applying a LINQ Expression
grabbing items from the List<T> where the Speed
property is greater than 55
Trang 2115.1 Understanding the Role of LINQ
15.2 A First Look at LINQ Query Expressions
15.3 LINQ and Generic Collections
15.4 LINQ and Nongeneric Collections
15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators
Trang 2215.4 LINQ and Nongeneric Collections
It is possible to iterate over data contained within
nongeneric collections using the generic
Enumerable.OfType<T>() method
Trang 23Nongeneric Collections
Use the previously defined Car type and import the
System.Collections namespace
Trang 2415.1 Understanding the Role of LINQ
15.2 A First Look at LINQ Query Expressions
15.3 LINQ and Generic Collections
15.4 LINQ and Nongeneric Collections
15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators
Trang 2515.5 The Internal Representation of LINQ
Query Operators
Introduced to the process of building query expressions using various C# query operators (such as from, in,
where, orderby, and select)
C# compiler actually translates these tokens into calls on various methods of the System.Linq.Enumerable type
Many methods require a generic delegate of type
Func<>
Trang 26Query Expressions
Trang 27Query Expressions
syntax
Trang 28Building Query Expressions with Query Operators
Building LINQ expressions using various query operators (from, in, where, orderby, etc.) is the most common and most straightforward approach
Trang 29Building Query Expressions Using the Enumerable Type and Lambdas
Enumerable type directly is much more verbose than making use of the C# query operators
Trang 30Building Query Expressions Using the
Enumerable Type and Anonymous Methods
Trang 31Building Query Expressions Using the Enumerable Type and Raw Delegates
Trang 32 Many methods of Enumerable require delegates (Func<> in particular) as parameters
Under C# 2008, any method requiring a delegate parameter can instead be passed a lambda expression
Lambda expressions are simply anonymous methods in
disguise (which greatly improve readability)
Trang 3315.1 Understanding the Role of LINQ
15.2 A First Look at LINQ Query Expressions
15.3 LINQ and Generic Collections
15.4 LINQ and Nongeneric Collections
15.5 The Internal Representation of LINQ Query Operators 15.6 Investigating the C# LINQ Query Operators
Trang 3415.6 Investigating the C# LINQ Query
Operators
from, in Used to define the backbone for any LINQ expression, which allows you to extract a subset of data from a
fitting container.
where Used to define a restriction for which items to extract from a containerselect Used to select a sequence from the container
join, on, equals, into Performs joins based on specified key Remember, these “joins” do not need to have anything to do with
data in a relational database.
Trang 35Building a New Test Project
FunWithLinqExpressions
Next, define a trivial Car type
Populate an array with the following Car objects within your Main() method
Trang 36Example 15.1 Investigating the C# LINQ Query Operators
Trang 37Basic Selection Syntax
Ordering of the operators is critical
var result = from item in container select item;
Trang 38Obtaining Subsets of Data
To obtain a specific subset from a container, use the
where operator
var result = from item in container where Boolean
expression select item;
Trang 39Projecting New Data Types
The compiler defines a only property and a only backing field for each specified name
read- Project new forms of data from an existing data source
Trang 40Reversing Result Sets
Using the generic Reverse<T>() method of the
Enumerable type
Trang 41Sorting Expressions
A query expression can take an orderby operator to sort items in the subset by a
specific value
To view the results in a descending order, simply include the descending operator
var result = from item in container orderby value ascending/descending select item;
Trang 421 static void OrderedResults(Car[] myCars)
3 // Order all the cars by PetName.
4 var subset = from c in myCars orderby c.PetName select c;
5 Console.WriteLine( "Ordered by PetName:" );
6 foreach (Car c in subset)
10 // Now find the cars that are going less than 55 mph,
11 // and order by descending PetName
12 subset = from c in myCars
13 where c.Speed > 55 orderby c.PetName descending select c;
14 Console.WriteLine( "\nCars going faster than 55, ordered by PetName:" );
15 foreach (Car c in subset)
Trang 43Finding Differences
obtaining a result set that determines the differences between
two IEnumerable<T> compatible containers
static void GetDiff()
var carDiff =(from c in myCars select c)
.Except(from c2 in yourCars select c2);
Console.WriteLine( "Here is what you don't have, but I do:" );
foreach ( string s in carDiff)
Console.WriteLine(s); // Prints Yugo.
}
Trang 44 LINQ is a set of related technologies that attempts to
provide a single, symmetrical manner to interact with
diverse forms of data
LINQ query expressions can return any number of result sets, it is common to make use of the var keyword to
represent the underlying data type
LINQ query operators are simply shorthand notations for making calls on static members of the
System.Linq.Enumerable type
Trang 45References