LINQ FUNCTIONS

LINQ provides several functions (implemented as extension methods) that are not supported by Visual Basic’s LINQ syntax. Though you cannot use these in LINQ queries, you can apply them to the results of queries to perform useful operations.

For example, the following code defines a query that looks for customers named Rod Stephens. It then applies the FirstOrDefault extension method to the query to return either the first object selected by the query or Nothing if the query selects no objects.

Dim rod_query = From cust In all_customers
    Where cust.LastName = "Stephens" AndAlso cust.FirstName = "Rod"
Dim rod As Person = rod_query.FirstOrDefault()

The following list describes some of the more useful of these extension methods:

  • Aggregate — Uses a function specified by the code to calculate a custom aggregate.
  • Concat — Concatenates two sequences into a new sequence.
  • Contains — Determines whether the result contains a specific value.
  • DefaultIfEmpty — If the query’s result is not empty, returns the result. If the result is empty, returns an IEnumerable containing a default value. Optionally can also specify the default value (for example, a new object rather than Nothing) to use if the query’s result is empty.
  • ElementAt — Returns an element at a specific position in the query’s result. If there is no element at that position, it throws an exception.
  • ElementAtOrDefault — Returns an element at a specific position in the query’s result. If there is no element at that position, it returns a default value for the data type.
  • Empty — Creates an empty IEnumerable.
  • Except — Returns the items in one IEnumerable that are not in a second IEnumerable.
  • First — Returns the first item in the query’s result. If the query contains no results, it throws an exception.
  • FirstOrDefault — Returns the first item in the query’s result. If the query contains no results, it returns a default value for the data type.
  • Intersection — Returns the intersection of two IEnumerable objects. In other words, it returns an IEnumerable containing items that are in both of the original IEnumerable objects.
  • Last — Returns the last item in the query’s result. If the query contains no results, it throws an exception.
  • LastOrDefault — Returns the last item in the query’s result. If the query contains no results, it returns a default value for the data type.
  • Range — Creates an IEnumerable containing a range of integer values.
  • Repeat — Creates an IEnumerable containing a value of a given type repeated a specific number of times.
  • SequenceEqual — Returns True if two sequences are identical.
  • Single — Returns the single item selected by the query. If the query does not contain exactly one result, it throws an exception.
  • SingleOrDefault — Returns the single item selected by the query. If the query contains no results, it returns a default value for the data type. If the query contains more than one item, it throws an exception.
  • Union — Returns the union of two IEnumerable objects. In other words, it returns an IEnumerable containing items that are in either of the original IEnumerable objects.

Example program FunctionExamples, which is available for download on the book’s website, demonstrates most of these functions. Example program SetExamples demonstrates Except, Intersection, and Union.

LINQ also provides functions that convert results into new data types. The following list describes these methods:

  • AsEnumerable — Converts the result into a typed IEnumerable(Of T).
  • AsQueryable — Converts an IEnumerable into an IQueryable.
  • OfType — Removes items that cannot be cast into a specific type.
  • ToArray — Places the results in an array.
  • ToDictionary — Places the results in a Dictionary using a selector function to set each item’s key.
  • ToList — Converts the result into a List(Of T).
  • ToLookup — Places the results in a Lookup (one-to-many dictionary) using a selector function to set each item’s key.

Note that the ToArray, ToDictionary, ToList, and ToLookup functions force the query to execute immediately instead of waiting until the program accesses the results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.13.76