a tour of new features introducing LINQ Agenda


















- Slides: 18
a tour of new features introducing LINQ
Agenda of LINQ Presentation • • LINQ Fundamentals Anonymous Functions/Lambda Expression Type Inference LINQ to SQL LINQ to XML LINQ to Entity We have features for every step of the way
1 LINQ FUNDAMENTALS Introduced in Visual Studio 2008 and. NET Framework version 3. 5
Getting Started with LINQ 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. All LINQ Query operation has three parts – 1. Obtain the data source 2. Create the Query 3. Execute the query
LINQ Queries and Query Operations Query variables just stores information is required to produce results whenspecifies query is how the Queries specifies what information to the retrieve from that the data source or sources. It also executed at later point. 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> Deferred Query Execution Forcing Immediate Execution Developer can force execution by putting foreach loop immediately after query expression.
Query Operations LINQ Queries and Query Operations Standard Query Operators Presents from, where, select and orderby clauses Presents Group By and Join clause
Type Relationships in LINQ Query Operations LINQ Queries and Query Operations Queries that do not transform source data. Queries that transform source data.
Benefits of LINQ This will cover the benefit of LINQ by using it in various scenarios covering LINQ to Object, LINQ to SQL, LINQ to XML.
2 BENEFITS OF LINQ Access In-memory data structures, SQL (Datasets, Entities, and SQL), XML Documents
LINQ to Objects 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) More complex the operation you want to Advantage of LINQ over for each loops: on data, the more benefit perform 1. More concise and readable, especially when filtering multiple conditions. developer realizes. 2. Provide powerful filtering, ordering and grouping capabilities with a minimum of 3. application code Can be porter to other data sources with little or no modification. Querying non-generic IEnumerable collection // C# var query = from Student s in arr. List. . . 'Visual Basic Dim query = From student As Student In arr. List. . . 1. 2. Explicitly declaration type of range variable is mandatory if using non-generic collection. 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 name
LINQ to Objects (Reflection) Query an assembly’s metadata with Reflection. This example uses the Get. Types 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 Method. Info array that is returned from the Get. Methods 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.
LINQ to SQL LINQ SQL run-time infrastructure and LINQs to SQL provides a run-time environment for to 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 design-time then translating tabular results back into objects you define. tools significantly reduce the LINQ to SQL 1. 2. 3. workload for the database application developer. Implement standard query operators for relational databases. Manages entity objects throughout their lifetime, aiding you in maintaining the integrity of your data Automating the process of translating your modification back into the store. Data. Context is the main conduit for retrieving objects from database, and changes. First Step – Declaringresubmit object classes, for representing application data Discuss how Object Identity is maintained.
LINQ to SQL Defining Relationships LINQs to SQL defines Association attribute you can apply to a member used to represent a LINQ to relationship SQL implementsisa one technique deferred loading in order to help maintain this illusion. When relationship. An associate like acalled foreign-key to primary-key relationship you query for an values object you actually only retrieve the objects you asked for. The related objects are not that is made by matching column between tables. automatically fetched at the same time. Querying Across Relationships
LINQ to SQL Modifying and Saving Entities LINQs to SQL is also designed to offer maximum flexibility in manipulating and persisting changes made to your objects. When Submit. Changes() is called, LINQ to SQL automatically generates and executes SQL Commands in order to transmit the changes back to SQL database. Queries ? ? ? ?
LINQ to SQL Reuse a connection between ADO. NET Command Data Context Performance can be improved by seeking read-only results by setting Object. Tracking. Enabled=false
LINQ to SQL Entity Life Cycle Tracking Changes Submitting Changes Simultaneous Changes Transactions
The End