link quries

24
M. Adil Khan 091420095 Ulfat Mushtaq 091420252 Zunair Nasir 091420166

Upload: ulfat-mushtaq

Post on 15-Jan-2017

37 views

Category:

Engineering


0 download

TRANSCRIPT

M. Adil Khan 091420095Ulfat Mushtaq 091420252

Zunair Nasir 091420166

Language Integrated Query in .NET (LINQ)

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

LINQ Architecture

Flavors of LINQ

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

LINQ to SQL ArchitectureApplication

LINQ to SQL

SQL Server

from c in db.Customerswhere c.City == "London"select c.CompanyName

Enumerate

SELECT CompanyNameFROM CustomerWHERE City = 'London'

SQL Queryor 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

Cont… 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.

Cont… 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>

Language Features that Enable LINQ

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

Query Operations All LINQ query operations consist

of three distinct actions:› Obtain the data source› Create the query› Execute the query

Fluent Syntax 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.

Type Inferencing 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; // Novar c = null; // No var c = default(string); // Yespublic var DoThis(int x){} // Nopublic void DoThis(var x){} // No

Extension MethodsExtension 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

Lambda Expressions 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:

Query Expressions 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

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

Deferred Execution

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:

How Deferred Execution Works Chaining Decorators Chaining query operators creates

a layering of decorators. Consider the following query:

In Deferred Execution Which Query Operators are Executed Immediately

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:

Captured Variables 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:

Advantages 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.

Demand-Driven Pull Model

Subqueries Subquery is a query contained within

another query’s lambda expression.

THANK YOU SO MUCH