3. LINQ
What is LINQ?
It is designed to work with all shapes and sizes
of different data and allow you to perform
Query, Set, and Transform operations on all
of it. Pretty much anything that implements
Enumerable is a target for LINQ. The basic
units of data in LINQ are sequences and
elements.
A sequence is any object that implements
IEnumerable<T> and an element is each item in
the sequence
4.
5. There are a variety of flavors of LINQ for
accessing and manipulating different data
sources.
LINQ to Objects
LINQ to DataSets
LINQ to SQL
LINQ to Entities
LINQ to XML
6. Application
LINQ to SQL
SQL Server
from c in db.Customers
where c.City == "London"
select c.CompanyName
Enumerate
SELECT CompanyName
FROM Customer
WHERE City = 'London'
SQL Query
or SProc
Rows
Objects
db.Customers.Add(c1);
c2.City = “Seattle";
db.Customers.Remove(c3);
SubmitChanges()
INSERT INTO Customer …
UPDATE Customer …
DELETE FROM Customer …
DML
or SProcs
7. In the Enumerable class in System.Linq, there are around
40 query operators—all implemented as static extension
methods. These are called standard query operators.
Queries that operate over local sequences are called local
queries or LINQ-to-objects queries.
LINQ also supports sequences that can be dynamically
fed from a remote data source such as a SQL Server.
These sequences additionally implement the
IQueryable<T>interface and are supported through a
matching set of standard query operators in the
Queryable class.
A query is an expression that, when enumerated,
transforms sequences with query operators.
8. The standard query operators provide query capabilities
including
› Filtering – where
› Projection – select, selectMany
› Aggregation – Sum, Max, Count, Average
› Sorting – orderby
› Grouping – groupby
› … and many more
A query operator is a method that transforms a sequence.
A typical query operator accepts an input sequence and emits a
transformed output sequence.
Two sets of LINQ standard operators
› Operating on IEnumerable<T>
› Operating on IQueryable<T>
9. LINQ makes heavy use of Generics.
Additionally, there were a number of features
added to the Visual Basic and C# languages
specifically to support LINQ.
Type inference
Extension Methods
Object initializer
Anonymous types
Lambda expressions
Query expressions
10. All LINQ query operations consist of
three distinct actions:
› Obtain the data source
› Create the query
› Execute the query
11. Fluent syntax is the most flexible and fundamental.
To build more complex queries, you append additional query
operators to the expression, creating a chain.
When query operators are chained as in this example, the output
sequence of one operator is the input sequence of the next.
12. Delcare Variable with “var” keyword
Compiler infers correct type
› Based on initialization
Only for local, (non-null) initialized variables
Customer c = new Customer(“Bob”, “Smith”, 1234);
var c = new Customer(“Bob”, “Smith”, 1234);
var c; // No
var c = null; // No
var c = default(string); // Yes
public var DoThis(int x){} // No
public void DoThis(var x){} // No
13. Extension methods enable you to add methods to a
data type or interface from outside the definition.
This feature enables you to, in effect, add new
methods to an existing type without actually
modifying the type.
Extends Existing Types
Adds Methods Without Derivation
Accesses Public Members of Extended Types
Must be:
› public and static
› Housed within a static class
14. Anonymous Methods
The query operator evaluates your lambda
expression upon demand—typically once
per element in the input sequence.
Lambda expressions allow you to feed your
own logic into the query operators.
Example:
15. C# provides a syntactic shortcut for writing
LINQ queries, called query expressions.
At compile time, query syntax is converted into
method calls to a LINQ provider's
16. Query expressions always start with a from
clause and end with either a select or group
clause.
The from clause declares a range variable (in
this case, n), which you can think of as
traversing the input sequence—rather like
foreach
17. An important feature of most query operators is that
they execute not when constructed, but when
enumerated (in other words, when MoveNext is
called on its enumerator). Consider the following
query:
18. Chaining Decorators
Chaining query operators creates a
layering of decorators. Consider the
following query:
19. All standard query operators provide deferred
execution, with the following exceptions:
› Operators that return a single element or scalar value,
such as First or Count
› The following conversion operators:
ToArray, ToList, ToDictionary, ToLookup
These operators cause immediate query execution
because their result types have no mechanism for
providing deferred execution. The Count method, for
instance, returns a simple integer, which doesn’t then
get enumerated. The following query is executed
immediately:
20. If your query’s lambda expressions capture
outer variables, the query will honor the
value of the those variables at the time the
query runs:
21. Familiar syntax for writing queries.
Compile-time checking for syntax errors and
type safety.
Improved debugger support.
Powerful filtering, ordering, and grouping
capabilities.
Consistent model for working with data
across various kinds of data sources and
formats.
22.
23. Subquery is a query contained within
another query’s lambda expression.