Language-Integrated Query (LINQ) : Content
Language-Integrated Query (LINQ) : Content
Language-Integrated Query
(LINQ)
Content
Introduction
LINQ to Objects
Henrk Kühll
Computer Science
Introduction
Henrk Kühll
Computer Science
Overview
Henrk Kühll
Computer Science
LINQ providers
Henrk Kühll
Computer Science
Henrk Kühll
Computer Science
The query
Henrk Kühll
Computer Science
Query execution
Henrk Kühll
Computer Science
• Query Expressions
– Queries expressions use a declarative syntax similar
to SQL or XQuery to query over IEnumerable
collections
• Implicitly Typed Variables (var)
– Instead of explicitly specifying a type when you
declare and initialize a variable, you can use the var
modifier to instruct the compiler to infer and assign
the type
Henrk Kühll
Computer Science
Henrk Kühll
Computer Science
• Anonymous Types
– An anonymous type is constructed by the compiler
and the type name is only available to the compiler
– Anonymous types are initialized with a new
expression and an object initializer, as shown here
Henrk Kühll
Computer Science
• Extension Methods
– An extension method is a static method that can be
associated with a type, so that it can be called as if it
were an instance method on the type
– This feature enables you to, in effect, "add" new
methods to existing types without actually modifying
them
– The standard query operators are a set of extension
methods that provide LINQ query functionality for
any type that implements IEnumerable<(Of
<(T>)>)
Henrk Kühll
Computer Science
• Auto-Implemented Properties
– Auto-implemented properties make property-
declaration more concise
public string Name {get; set;}
– When you declare a property as shown above, the
compiler converts this definition to a private field and a
default implementation
private string _name;
public string Name {
get {return this._name;}
set {this._name = value;}
}
Henrk Kühll
Computer Science
• Lambda Expressions
– A typical method consists of a return type, a method
name, a list of parameters, and a method body
– A lambda expression contains two of these elements - a
list of parameters and a method body:
(x, y) => { return x * y; }
– The left side of the lambda operator (=>) is the list of
parameters, and the right side is the method body
– The type of the parameters and the return value are
usually inferred from the context in which the lambda
expression is used (however, parameter types can be
stated explicitly)
– In LINQ programming, you will encounter lambda
expressions when you use method syntax
Henrk Kühll
Computer Science
Method syntax
Henrk Kühll
Computer Science
LINQ to Objects
Henrk Kühll
Computer Science
var queryAllCustomers =
from cust in customers
select cust;
Henrk Kühll
Computer Science
var queryBikeCustomers =
from cust in customers
where cust.CompanyName == "A Bike Store" || cust.
CompanyName == "Bike World"
select cust;
Henrk Kühll
Computer Science
var queryFitnessCustomers =
from cust in customers
where cust.CompanyName == "Fitness
Hotel"
orderby cust.FirstName ascending
select cust;
Henrk Kühll
Computer Science
// queryCustomersByCompany is an
// IEnumerable<IGrouping<string, Customer>>
var queryCustomersByCompany =
from cust in customers
group cust by cust.CompanyName;
Henrk Kühll
Computer Science
Henrk Kühll
Computer Science
var custJoinAddr =
from a in adresses
join c in customers
on a.CompanyName equals c.CompanyName
select new { c.FirstName, c. LastName, a.City}
Henrk Kühll
Computer Science
• You can use the select clause to create output sequences whose
elements consist of only one or several properties of each element
in the source sequence, or output sequences whose elements
consist of the results of operations performed on the source data:
select cust.FirstName;
select new {FirstName=cust.FirstName,
LastName=cust.LastName};
select new Customer {FirstName=cust.FirstName,
LastName=cust.LastName};
select String.Format(” FirstName={0}, LastName={1},
cust. FirstName, cust. LastName”);
Henrk Kühll
Computer Science
LINQ to SQL
Henrk Kühll
Computer Science
Overview
Henrk Kühll