Linq
Upcoming SlideShare
Loading in...5
×
 

Linq

on

  • 790 views

 

Statistics

Views

Total Views
790
Views on SlideShare
790
Embed Views
0

Actions

Likes
1
Downloads
48
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Linq Linq Presentation Transcript

  • LINQ Language-Integrated Query
  • Table of content
    • What is LINQ
    • Language Features that Enable LINQ
    • Flavors of LINQ
    • LINQ Architecture
    • LINQ to Objects
      • Query expressions
      • Implicitly Typed Variables
      • Object initializer
      • Anonymous types
      • Extension Methods
      • Lambda expressions
      • More on Queries
  • Table of content
    • LINQ to Dataset
      • Single-Table Queries (LINQ to DataSet)
      • Cross-Table Queries (LINQ to DataSet)
      • Accessing Values without using Field method
      • Accessing Values using Field method
      • Advantage of using Field method and SetField method
      • Querying Typed DataSets
  • What is LINQ
    • LINQ (often overheard pronounced as "link") is a step in the evolution of data access. It is a programming model that brings a much needed uniformity to accessing data from files, XML, database, registry, event log, and a whole host of other sources of data such as Active Directory and even services from 3rd parties such as Flickr.
    • 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 IEnumerable is a target for LINQ.
  • 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. A couple of my more recent articles introduced some of these language features as a precursor to LINQ. The following contains a partial list of the language features that help enable LINQ and a brief description of each:
    • Type inference : Shorthand indicating the variables type is the compile time type of the right hand assignment
    • Extension Methods : Extending an existing value or reference type without deriving a new type
    • Object initializer : Short form of object initialization syntax that generates the equivalent code
    • Anonymous types : Create statements without constructing a method or type
    • Lambda expressions : Concise way of creating inline methods
    • Query expressions : SQL-like statements within code for manipulating objects
  • Flavors of LINQ
    • There are a variety of flavors of LINQ for accessing and manipulating different data sources. The trailing list contains some of the data domains provided by Microsoft. Some of these will be topics of future .NET Nuts and Bolts articles.
    • LINQ to Objects : Manipulates collections of objects
    • LINQ to DataSets : Manipulates a DataSet using LINQ
    • LINQ to SQL : Maps between custom types and a physical database table schema
    • LINQ to Entities : Uses a conceptual Entity Data Model to create a conceptual model of a physical database
    • LINQ to XML: Allows querying and manipulation of XML
  • LINQ Architecture
  • LINQ to Objects
  • LINQ to Objects The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable  or IEnumerable(Of T)  collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML . You can use LINQ to query any enumerable collections such as List(Of T) , Array , or Dictionary(Of TKey , TValue ) . The collection may be user-defined or may be returned by a .NET Framework API. In a basic sense, LINQ to Objects represents a new approach to collections. In the old way, you had to write complex foreach loops that specified how to retrieve data from a collection. In the LINQ approach, you write declarative code that describes what you want to retrieve. In addition, LINQ queries offer three main advantages over traditional foreach loops:
    • They are more concise and readable, especially when filtering multiple conditions.
    • They provide powerful filtering, ordering, and grouping capabilities with a minimum of application code.
    • They can be ported to other data sources with little or no modification.
  • Language Features That Support LINQ Query Expressions Query expressions in Visual Basic 2008 can be expressed in a declarative syntax similar to that of SQL or XQuery. At compile time, query syntax is converted into method calls to a LINQ provider's implementation of the standard query operator extension methods. Applications control which standard query operators are in scope by specifying the appropriate namespace with an Imports statement. Syntax for a Visual Basic query expression looks like this: Dim londonCusts = From cust In customers _ Where cust.City = "London" _ Order By cust.Name Ascending _ Select cust.Name, cust.Phone
  • LINQ Query Syntax Shaping the output select <expression> Projection Aggregate the source items count([<expression>]), sum(<expression>), min(<expression>), max (<expression>), avg(<expression>) Aggregate Control the ordering of the results order by <expression>, <expression> [Ascending | Descending] Order Expression specifying the selection Criteria where <expression>, distinct Filter Information source providing a set of item (s) from <item> in <data source> Source Using type inference to assign the resulting value(s) var <variable> = Destination
  • Language Features That Support LINQ Implicitly Typed Variables Instead of explicitly specifying a type when you declare and initialize a variable, you can now enable the compiler to infer and assign the type, as shown in the following example. This is referred to as local type inference. ' The variable number will be typed as an integer. Dim aNumber = 5 ' The variable name will be typed as a String . Dim aName = &quot;Virginia&quot;
  • Language Features That Support LINQ Object Initializers Object initializers are used in query expressions when you have to create an anonymous type to hold the results of a query. They also can be used to initialize objects of named types outside of queries. By using an object initializer, you can initialize an object in a single line without explicitly calling a constructor. Assuming that you have a class named Customer that has public Name and Phone properties, along with other properties, an object initializer can be used in this manner: Dim aCust As Customer = New Customer With {.Name = &quot;Mike&quot;, _ .Phone = &quot;555-0212&quot;}
  • Language Features That Support LINQ Anonymous Types Anonymous types provide a convenient way to temporarily group a set of properties into an element that you want to include in a query result. This enables you to choose any combination of available fields in the query, in any order, without defining a named data type for the element. An anonymous type is constructed dynamically by the compiler. The name of the type is assigned by the compiler, and it might change with each new compilation. Therefore, the name cannot be used directly. Anonymous types are initialized in the following way: ' Outside a query. Dim product = New With {.Name = &quot;paperclips&quot;, .Price = 1.29} ' Inside a query. ' You can use the existing member names of the selected fields, as was ' shown previously in the Query Expressions section of this topic. Dim londonCusts1 = From cust In customers_ Where cust.City = &quot;London“ _ Select cust.Name, cust.Phone ' Or you can specify new names for the selected fields. Dim londonCusts2 = From cust In customers _ Where cust.City = &quot;London&quot; _ Select CustomerName = cust.Name, _ CustomerPhone = cust.Phone
  • Language Features That Support LINQ Extension Methods 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. The standard query operators are themselves a set of extension methods that provide LINQ query functionality for any type that implements IEnumerable(Of T) . Other extensions to IEnumerable(Of T) include  Count , Union , and Intersect . ' Import System.Runtime.CompilerServices to use the Extension attribute. <Extension()> _ Public Sub Print(ByVal str As String) Console.WriteLine(str) End Sub Dim greeting As String = &quot;Hello&quot; greeting.Print() The following extension method adds a print method to the String class. The method is called like an ordinary instance method of String :
  • Language Features That Support LINQ Lambda Expressions A lambda expression is a function without a name that calculates and returns a single value. Unlike named functions, a lambda expression can be defined and executed at the same time. The following example displays 4. Console.WriteLine((Function(num As Integer) num + 1)(3)) You can assign the lambda expression definition to a variable name and then use the name to call the function. The following example also displays 4. Dim add1 = Function(num As Integer) num + 1 Console.WriteLine(add1(3)) In LINQ, lambda expressions underlie many of the standard query operators. The compiler creates lambda expressions to capture the calculations that are defined in fundamental query methods such as Where, Select, Order By, Take While, and others. For example, the following code defines a query that returns all senior students from a list of students. Dim seniorsQuery = From stdnt In students Where stdnt.Year = &quot;Senior&quot; Select stdnt The query definition is compiled into code that is similar to the following example, which uses two lambda expressions to specify the arguments for Where and Select. Dim seniorsQuery2 = students Where(Function(st) st.Year = &quot;Senior&quot;) Select(Function(s) s)
  • More On Queries Like normal SQL queries we can do the following operations by using LINQ expressions Describes the Skip clause, which bypasses a specified number of elements in a collection and then returns the remaining elements. Skip Clause Describes the Select clause, which declares a set of range variables for a query Select Clause Describes the Order By clause, which specifies the sort order for columns in a query. Order By Clause Describes the Let clause, which computes a value and assigns it to a new variable in the query. Let Clause Describes the Join clause, which combines two collections into a single collection. Join Clause Describes the Group Join clause, which combines two collections into a single hierarchical collection. Group Join Clause Describes the Group By clause, which groups the elements of a query result and can be used to apply aggregate functions to each group. Group By Clause Describes the From clause, which specifies a collection and a range variable for a query. From Clause Describes the Distinct clause, which restricts the values of the current range variable to eliminate duplicate values in query results. Distinct Clause Describes the Aggregate clause, which applies one or more aggregate functions to a collection Aggregate Clause Descriptions Clause Name
  • More On Queries Like normal SQL queries we can do the following operations by using LINQ expressions Describes the Skip While clause, which bypasses elements in a collection as long as a specified condition is true and then returns the remaining elements. Skip While Clause Describes the Where clause, which specifies a filtering condition for a query. Where Clause Describes the Take While clause, which includes elements in a collection as long as a specified condition is true and bypasses the remaining elements. Take While Describes the Take clause, which returns a specified number of contiguous elements from the start of a Collection Take Descriptions Clause Name
  • Queries – Order By Clause Specifies the sort order for a query result. Syntax: Order By orderExp1 [ Ascending | Descending ] [, orderExp2 [...] ] Example The following query expression uses a From clause to declare a range variable book for the books collection. The Order By clause sorts the query result by price in ascending order (the default). Books with the same price are sorted by title in ascending order. The Select clause selects only the Title property as the value returned by the query. Dim titlesAscendingPrice = From book In books Order By book.Price, book.Title Select book.Title, book.Price The following query expression uses the Order By clause to sort the query result by price in descending order. Books with the same price are sorted by title in ascending order. Dim titlesDescendingPrice = From book In books Order By book.Price Descending, book.Title _ Select book.Title, book.Price Dim bookOrders = From book In books Select book.Title, book.Price, book.PublishDate, book.Author _ Order By Author, Title, Price
  • Queries – Distinct By Clause Restricts the values of the current range variable to eliminate duplicate values in subsequent query clauses. You can use the Distinct clause to return a list of unique items. The Distinct clause causes the query to ignore duplicate query results. The Distinct clause applies to duplicate values for all return fields specified by the Select clause. If no Select clause is specified, the Distinct clause is applied to the range variable for the query identified in the From clause. If the range variable is not an immutable type, the query will only ignore a query result if all members of the type match an existing query result. Example The following query expression joins a list of customers and a list of customer orders. The Distinct clause is included to return a list of unique customer names and order dates. Dim customerOrders = From cust In customers, ord In orders _ Where cust.CustomerID = ord.CustomerID _ Select cust.CompanyName, ord.OrderDate Distinct
  • Queries – From Clause Specifies one or more range variables and a collection to query. Syntax: From element [ As type ] In collection [ _ ] [, element2 [ As type2 ] In collection2 [, ... ] ] Example: Dim customersForRegion = From cust In customers You can specify multiple From clauses in a query to identify multiple collections to be joined. When multiple collections are specified, they are iterated over independently, or you can join them if they are related. You can join collections implicitly by using the Select clause, or explicitly by using the Join or Group Join clauses. As an alternative, you can specify multiple range variables and collections in a single From clause, with each related range variable and collection separated from the others by a comma. The following code example shows both syntax options for the From clause.
  • Queries – From Clause Continue … ' Multiple From clauses in a query. Dim result = From var1 In collection1, var2 In collection2 ' Equivalent syntax with a single From clause. Dim result2 = From var1 In collection1 _ From var2 In collection2 The From clause defines the scope of a query, which is similar to the scope of a For loop. Therefore, each element range variable in the scope of a query must have a unique name. Because you can specify multiple From clauses for a query, subsequent From clauses can refer to range variables in the From clause, or they can refer to range variables in a previous From clause. For example, the following example shows a nested From clause where the collection in the second clause is based on a property of the range variable in the first clause. Dim allOrders = From cust In GetCustomerList() _ From ord In cust.Orders _ Select ord Dim customersForRegion = From cust In customers _ Where cust.Region = region
  • LINQ to Dataset
  • LINQ to Dataset
  • Queering Dataset Coverage Areas
    • Single-Table Queries (LINQ to DataSet)
    • Describes how to perform single-table queries.
    • Cross-Table Queries (LINQ to DataSet)
    • Describes how to perform cross-table queries.
    • Querying Typed DataSets
    • Describes how to query typed DataSet objects.
    Language-Integrated Query (LINQ) queries work on data sources that implement the IEnumerable(Of T)  interface or the IQueryable interface. The DataTable  class does not implement either interface, so you must call the  AsEnumerable method if you want to use the DataTable  as a source in the From clause of a LINQ query.
  • Queering Dataset Single Table Queries The following example gets all the online orders from the SalesOrderHeader table and outputs the order ID, order date, and order number to the console. ' Fill the DataSet. Dim ds As New DataSet() ds.Locale = CultureInfo.InvariantCulture ' See the FillDataSet method in the Loading Data Into a DataSet topic. FillDataSet(ds) Dim orders As DataTable = ds.Tables(&quot;SalesOrderHeader&quot;) Dim query = From order In orders.AsEnumerable() _ Where order.Field(Of Boolean)(&quot;OnlineOrderFlag&quot;) = True _ Select New With { _ .SalesOrderID = order.Field(Of Integer)(&quot;SalesOrderID&quot;), _ .OrderDate = order.Field(Of DateTime)(&quot;OrderDate&quot;), _ .SalesOrderNumber = order.Field(Of String)(&quot;SalesOrderNumber&quot;) _ } For Each onlineOrder In query Console.Write(&quot;Order ID: &quot; & onlineOrder.SalesOrderID) Console.Write(&quot; Order date: &quot; & onlineOrder.OrderDate) Console.WriteLine(&quot; Order number: &quot; & onlineOrder.SalesOrderNumber) Next
  • Queering Dataset Accessing Values without using Field method Dim products As DataTable = ds.Tables(&quot;Product&quot;) Dim query = _ From product In products.AsEnumerable() _ Where product!Color IsNot DBNull.Value AndAlso product!Color = &quot;Red&quot; _ Select New With _ { _ .Name = product!Name, _ .ProductNumber = product!ProductNumber, _ .ListPrice = product!ListPrice _ } For Each product In query Console.WriteLine(&quot;Name: &quot; & product.Name) Console.WriteLine(&quot;Product number: &quot; & product.ProductNumber) Console.WriteLine(&quot;List price: $&quot; & product.ListPrice & vbNewLine) Next
  • Queering Dataset Accessing Values using Field method Dim products As DataTable = ds.Tables(&quot;Product&quot;) Dim query = _ From product In products.AsEnumerable() _ Where product.Field(Of String)(&quot;Color&quot;) = &quot;Red&quot; _ Select New With _ { _ .Name = product.Field(Of String)(&quot;Name&quot;), _ .ProductNumber = product.Field(Of String)(&quot;ProductNumber&quot;), _ .ListPrice = product.Field(Of Decimal)(&quot;ListPrice&quot;) _ } For Each product In query Console.WriteLine(&quot;Name: &quot; & product.Name) Console.WriteLine(&quot;Product number: &quot; & product.ProductNumber) Console.WriteLine(&quot;List price: $ &quot; & product.ListPrice & vbNewLine) Next
  • Queering Dataset Advantage of using Field method and SetField method The Field method provides access to the column values of a DataRow and the SetField sets column values in a DataRow . Both the Field method and SetField method handle nullable types, so you do not have to explicitly check for null values as in the previous example. Both methods are generic methods, also, so you do not have to cast the return type.
  • Queering Dataset Cross-Table Queries The Language-Integrated Query (LINQ) framework provides two join operators,  Join  and GroupJoin . These operators perform equi-joins: that is, joins that match two data sources only when their keys are equal. (By contrast, Transact-SQL supports join operators other than equals, such as the less than operator.)  In relational database terms, Join implements an inner join. An inner join is a type of join in which only those objects that have a match in the opposite data set are returned. The GroupJoin operators have no direct equivalent in relational database terms; they implement a superset of inner joins and left outer joins. A left outer join is a join that returns each element of the first (left) collection, even if it has no correlated elements in the second collection.
  • Queering Dataset Cross-Table Queries Dim orders As DataTable = ds.Tables(&quot;SalesOrderHeader&quot;) Dim details As DataTable = ds.Tables(&quot;SalesOrderDetail&quot;) Dim query = _ From order In orders.AsEnumerable() _ Join detail In details.AsEnumerable() _ On order.Field(Of Integer)(&quot;SalesOrderID&quot;) Equals _ detail.Field(Of Integer)(&quot;SalesOrderID&quot;) _ Where order.Field(Of Boolean)(&quot;OnlineOrderFlag&quot;) = True And _ order.Field(Of DateTime)(&quot;OrderDate&quot;).Month = 8 _ Select New With _ { _ .SalesOrderID = order.Field(Of Integer)(&quot;SalesOrderID&quot;), _ .SalesOrderDetailID = detail.Field(Of Integer)(&quot;SalesOrderDetailID&quot;), _ .OrderDate = order.Field(Of DateTime)(&quot;OrderDate&quot;), _ .ProductID = detail.Field(Of Integer)(&quot;ProductID&quot;) _ } For Each order In query Console.WriteLine(order.SalesOrderID & vbTab & _ order.SalesOrderDetailID & vbTab & _ order.OrderDate & vbTab & _ order.ProductID) Next
  • Queering Dataset Querying Typed DataSets A typed DataSet is a class that derives from a DataSet. As such, it inherits all the methods, events, and properties of a DataSet. Additionally, a typed DataSet provides strongly typed methods, events, and properties. This means you can access tables and columns by name, instead of using collection-based methods. Aside from the improved readability of the code, a typed DataSet also allows the Visual Studio .NET code editor to automatically complete lines as you type. Additionally, the strongly typed DataSet provides access to values as the correct type at compile time. With a strongly typed DataSet, type mismatch errors are caught when the code is compiled rather than at run time. Typed DataSets LINQ to DataSet also supports querying over a typed DataSet . With a typed DataSet , you do not have to use the generic Field method or SetField method to access column data.  Property names are available at compile time because the type information is included in the DataSet . LINQ to DataSet provides access to column values as the correct type, so that type mismatch errors are caught when the code is compiled instead of at run time.
  • Queering Dataset Querying Typed DataSets Dim orders = ds.Tables(&quot;SalesOrderHeader&quot;) Dim query = _ From o In orders _ Where o.OnlineOrderFlag = True _ Select New {SalesOrderID := o.SalesOrderID, _ OrderDate := o.OrderDate, _ SalesOrderNumber := o.SalesOrderNumber} For Each Dim onlineOrder In query Console.WriteLine(&quot;{0}t{1:d}t{2}&quot;, _ onlineOrder.SalesOrderID, _ onlineOrder.OrderDate, _ onlineOrder.SalesOrderNumber) Next