10. 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
11. 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
12. 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 = "Virginia"
13. 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 = "Mike", _ .Phone = "555-0212"}
14. 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 = "paperclips", .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 = "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 = "London" _ Select CustomerName = cust.Name, _ CustomerPhone = cust.Phone
15. 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 = "Hello" 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 :
16. 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 = "Senior" 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 = "Senior") Select(Function(s) s)
17. 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
18. 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
19. 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
20. 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
21. 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.
22. 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
26. 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("SalesOrderHeader") Dim query = From order In orders.AsEnumerable() _ Where order.Field(Of Boolean)("OnlineOrderFlag") = True _ Select New With { _ .SalesOrderID = order.Field(Of Integer)("SalesOrderID"), _ .OrderDate = order.Field(Of DateTime)("OrderDate"), _ .SalesOrderNumber = order.Field(Of String)("SalesOrderNumber") _ } For Each onlineOrder In query Console.Write("Order ID: " & onlineOrder.SalesOrderID) Console.Write(" Order date: " & onlineOrder.OrderDate) Console.WriteLine(" Order number: " & onlineOrder.SalesOrderNumber) Next
27. Queering Dataset Accessing Values without using Field method Dim products As DataTable = ds.Tables("Product") Dim query = _ From product In products.AsEnumerable() _ Where product!Color IsNot DBNull.Value AndAlso product!Color = "Red" _ Select New With _ { _ .Name = product!Name, _ .ProductNumber = product!ProductNumber, _ .ListPrice = product!ListPrice _ } For Each product In query Console.WriteLine("Name: " & product.Name) Console.WriteLine("Product number: " & product.ProductNumber) Console.WriteLine("List price: $" & product.ListPrice & vbNewLine) Next
28. Queering Dataset Accessing Values using Field method Dim products As DataTable = ds.Tables("Product") Dim query = _ From product In products.AsEnumerable() _ Where product.Field(Of String)("Color") = "Red" _ Select New With _ { _ .Name = product.Field(Of String)("Name"), _ .ProductNumber = product.Field(Of String)("ProductNumber"), _ .ListPrice = product.Field(Of Decimal)("ListPrice") _ } For Each product In query Console.WriteLine("Name: " & product.Name) Console.WriteLine("Product number: " & product.ProductNumber) Console.WriteLine("List price: $ " & product.ListPrice & vbNewLine) Next
29. 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.
30. 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.
31. Queering Dataset Cross-Table Queries Dim orders As DataTable = ds.Tables("SalesOrderHeader") Dim details As DataTable = ds.Tables("SalesOrderDetail") Dim query = _ From order In orders.AsEnumerable() _ Join detail In details.AsEnumerable() _ On order.Field(Of Integer)("SalesOrderID") Equals _ detail.Field(Of Integer)("SalesOrderID") _ Where order.Field(Of Boolean)("OnlineOrderFlag") = True And _ order.Field(Of DateTime)("OrderDate").Month = 8 _ Select New With _ { _ .SalesOrderID = order.Field(Of Integer)("SalesOrderID"), _ .SalesOrderDetailID = detail.Field(Of Integer)("SalesOrderDetailID"), _ .OrderDate = order.Field(Of DateTime)("OrderDate"), _ .ProductID = detail.Field(Of Integer)("ProductID") _ } For Each order In query Console.WriteLine(order.SalesOrderID & vbTab & _ order.SalesOrderDetailID & vbTab & _ order.OrderDate & vbTab & _ order.ProductID) Next
32. 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.
33. Queering Dataset Querying Typed DataSets Dim orders = ds.Tables("SalesOrderHeader") 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("{0}{1:d}{2}", _ onlineOrder.SalesOrderID, _ onlineOrder.OrderDate, _ onlineOrder.SalesOrderNumber) Next