introducing linq
Post on 10-Jan-2016
158 Views
Preview:
DESCRIPTION
TRANSCRIPT
a tour of new features
introducing
LINQ
Agenda of LINQ Presentation
We have features for every step of the way
• LINQ Fundamentals• Anonymous Functions/Lambda Expression• Type Inference• LINQ• LINQ to SQL• LINQ to XML• LINQ to Entity
LINQ Fundamentals
Introduced in Visual Studio 2008 and .NET Framework version 3.5
All LINQ Query operation has three parts –1.Obtain the data source2.Create the Query3.Execute the query
Bridges the gap between world of objects and data.•Facilitates compile time type checking, and Intellisense•No need to learn different query language based on data source: SQL Database, XML documents, various web services, ADO.NET Datasets and any collection of objects that support IEnumerable<T> interface.
Getting Started with LINQ
LIN
Q Q
uerie
s an
d Q
uery
O
pera
tions
LINQ QueriesQueries specifies what information to retrieve from the data source or sources. It also specifies how the information should be sorted, grouped, and shaped before it is returned.
var query_variable = from ****<data source> where ****<filter expression> select **** <type of returned elements>
Query variables just stores the information that is required to produce results when query is executed at later point.
Deferred Query Execution
Forcing Immediate Execution
Developer can force execution by putting foreach loop immediately after query expression.
LIN
Q Q
uerie
s an
d Q
uery
O
pera
tions
Query OperationsStandard Query OperatorsStandard Query Operators
Presents from, where, select and orderby clausesPresents from, where, select and orderby clauses
Presents Group By and Join clausePresents Group By and Join clause
LIN
Q Q
uerie
s an
d Q
uery
O
pera
tions
Type Relationships in LINQ Query Operations
Queries that do not transform source data.Queries that do not transform source data. Queries that transform source data.Queries that transform source data.
This will cover the benefit of LINQ by using it in various scenarios covering LINQ to Object, LINQ to SQL, LINQ to XML.
Benefits of LINQ
Benefits of LINQ
Access In-memory data structures, SQL (Datasets, Entities, and SQL), XML Documents
LINQs to Object refers to the use of LINQ Queries with any Ienumerable or Ienumerable<T> collection directly, without the use of an intermediate LINQ provider (LINQ to SQL, LINQ to XML)
LINQ to Objects
Advantage of LINQ over for each loops:1.More concise and readable, especially when filtering multiple conditions.2.Provide powerful filtering, ordering and grouping capabilities with a minimum of application code3.Can be porter to other data sources with little or no modification.
More complex the operation you want to perform on data, the more benefit
developer realizes.
More complex the operation you want to perform on data, the more benefit
developer realizes.
Querying non-generic IEnumerable collectionQuerying non-generic IEnumerable collection
// C#var query = from Student s in arrList... 'Visual BasicDim query = From student As Student In arrList ...
1. Explicitly declaration type of range variable is mandatory if using non-generic collection.
2. Use of explicit declaration is equivalent to calling cast<TResult> method. Cast<TResult> will throw exception if the specified cast can not be performed.
LINQ to Objects (File Directories)Query for a file with special attribute or nameQuery for a file with special attribute or name
LINQ to Objects (Reflection)Query an assembly’s metadata with Reflection.Query an assembly’s metadata with Reflection.
This example uses the GetTypes method to return an array of types in the
specified assembly. The where filter is applied so that only public types are
returned. For each public type, a subquery is generated by using the
MethodInfo array that is returned from the GetMethods call. These results are filtered to return only those methods whose return type is an array or else a type that implements IEnumerable<T>
Finally, these results are grouped by using the type name as a key.
This example uses the GetTypes method to return an array of types in the
specified assembly. The where filter is applied so that only public types are
returned. For each public type, a subquery is generated by using the
MethodInfo array that is returned from the GetMethods call. These results are filtered to return only those methods whose return type is an array or else a type that implements IEnumerable<T>
Finally, these results are grouped by using the type name as a key.
LINQs to SQL provides a run-time environment for managing relational data as objects, without loosing the ability to query. It does this by tranlating LINQ queries to SQL for execution by the database, and then translating tabular results back into objects you define.
LINQ to SQLLINQ to SQL run-time infrastructure and
design-time tools significantly reduce the workload for the database application
developer.
LINQ to SQL run-time infrastructure and design-time tools significantly reduce the
workload for the database application developer.
First Step – Declaring object classes, for representing application dataFirst Step – Declaring object classes, for representing application data
DataContext is the main conduit for retrieving objects from database, and
resubmit changes.
DataContext is the main conduit for retrieving objects from database, and
resubmit changes.
Discuss how Object Identity is maintained.
Discuss how Object Identity is maintained.
LINQ to SQL
1.Implement standard query operators for relational databases.2.Manages entity objects throughout their lifetime, aiding you in maintaining the integrity of your data3.Automating the process of translating your modification back into the store.
LINQ to SQLDefining RelationshipsDefining Relationships
LINQs to SQL defines Association attribute you can apply to a member used to represent a relationship. An associate relationship is one like a foreign-key to primary-key relationship that is made by matching column values between tables.
Querying Across RelationshipsQuerying Across Relationships
LINQ to SQL implements a technique called deferred loading in order to help maintain this illusion. When you query for an object you actually only retrieve the objects you asked for. The related objects are not
automatically fetched at the same time.
LINQ to SQLModifying and Saving EntitiesModifying and Saving Entities
LINQs to SQL is also designed to offer maximum flexibility in manipulating and persisting changes made to your objects.
Queries ????????Queries ????????
When SubmitChanges() is called, LINQ to SQL automatically generates and executes SQL Commands in order to transmit the changes back to SQL database.
LINQ to SQLReuse a connection between ADO.NET Command and Data ContextReuse a connection between ADO.NET Command and Data Context
Performance can be improved by seeking read-only results by setting
ObjectTrackingEnabled=false
LINQ to SQLEntity Life CycleEntity Life Cycle
Tracking Changes Tracking Changes
Submitting Changes Submitting Changes
Simultaneous Changes Simultaneous Changes
TransactionsTransactions
top related