In our
introduction to LINQ, we saw how to create a LINQ statement by declaring
a variable using the Dim keyword. The Dim keyword lets the
compiler figure out what type of value the statement would produce. To
assist you with creating LINQ statements, the .NET Framework provides many
interfaces and classes. The interfaces and the classes that implement them
are created in the System.Linq namespace defined in the
System.Core.dll assembly library.

The IEnumerable Interface

Of the classes that provide support for LINQ is the
IEnumerable generic interface. In reality, a LINQ statement is an
object of type IEnumerable. Based on this, instead of the Dim
keyword, you can declare the statement using that interface. Inside the
parentheses, enter the type of list that the collection variable contains.
Here is an example:

Instead of processing a query locally, you can hand
the job to an external method that would return the query. To create a
method that returns a query, specify its return type as
IEnumerable. If the method will receive the value it must
process, you can pass it as argument. In the body of the method, create a
(the) LINQ statement(s) as you see fit. Before the method closes, make
sure it returns a value that either is of type IEnumerable
or implements that interface. Here is an example:

In this example, we used a method that returned a list
of simple values. In the same way, you can create a method that returns a
list of objects. You can also create a method that returns a single value
from a query.

Characteristics of Enumerable

Introduction

Besides, or instead of, the Select
statement in LINQ, the IEnumerable interface provides its own
mechanism for selecting the values. This can be done using the Select()
method that is overloaded with two versions whose syntaxes are:

This method expects a variable that would hold the
result of the selection. You can pass it a lambda expression. You don't
have to specify the TSource and the TResult
classes. After doing this, the IEnumerable variable can be used in
a foreach loop. Here is an example:

If you have a simple list of values of a primitive
type and the LINQ variable is declared using the var keyword, we
saw that you could sort it using the orderby operator. If the list
is made of values that each is based on a class, you can also sort it the
same way, in the IEnumerable expression. Here is an example:

Imagine you want to give the user the ability to sort
the list but you want the list to be accessible from sections of the code.
One way you can solve this problem is to declare a LINQ variable outside
of the code sections but without initializing the variable, as long as you
make sure the variable would be initialized before it is used. The problem
with the Dim keyword is that you must initialize its variable when
you declare it. The advantage with an IEnumerable variable is that
you do not have to initialize it when declaring it. You can declare it,
then specify its value in another section of the code.

LINQ and Numbers

A Range of Numbers

We know how to create an array of (random) natural
numbers and store it in a variable to use a query. Here is an example:

In some cases, you may want to work on a consecutive
list of numbers such as 1, 2, 3, 4, 5, 6, 7, and 8. Instead of declaring a
formal variable, the Enumerable class provides a method
named Range that allows you to specify the first number of
a range and a count of consecutive numbers to add to create a range. The
syntax of the Enumerable.Range() method is:

The first argument passed to this method is the
beginning of the range. The second argument specifies how many numbers to
add consecutively from the first. To use this method, you can declare a
variable of type IEnumerable and assign a call to
Enumerable.Range() that receives both arguments. Here is an
example:

Imports System.Linq
Imports System.Collections.Generic
Module Exercise
Public Function Main() As Integer
Dim Range As IEnumerable(Of Integer) = Enumerable.Range(22, 8)
Dim number = From N In Range Select N
For Each Member In number
Console.WriteLine(Member)
Next
Console.WriteLine()
Return 0
End Function
End Module

You can also declare the variable as type var.
You would receive the same result:

If you want to restrict the result, you can add a
where condition to it. Here is an example:

Imports System.Linq
Imports System.Collections.Generic
Module Exercise
Public Function Main() As Integer
Dim Range = Enumerable.Range(96, 10)
Dim number = From N
In Range
Where N Mod 2 = 0
Select N
For Each Member In number
Console.WriteLine(Member)
Next
Console.WriteLine()
Return 0
End Function
End Module

This would produce:

Counting the Number of Records

When you create a LINQ statement, it produces a list.
Although the list is of type IEnumerable, since this is only an
interface, the result relies on an actual class to provide its
characteristics. The class that gives you information about a result is
called Enumerable.

The Enumerable class in defined in the
System.Linq namespace that is part of the System.Core.dll
assembly. It is actually the Enumerable class that implements the
methods declared in the IEnumerable interface. Because the
Enumerable class is extremely big, we cannot review all of its methods.
We will use them as we move on and when a particular method becomes
necessary.

As mentioned already, a LINQ statement produces an
Enumerable list. You can then use that result to access a method of the
class. For example, the IEnumerable.Count() method is used to know
the number of items in the resulting list. You can access it from the
resulting list. Here is an example:

Remember that you can still use the Dim keyword
to declare the variable that would hold the resulting list. The same Dim
keyword can be used for the result of a method call. Here are examples:

Dim Empls As IEnumerable(Of Employee) = From StaffMembers
In Employees
Order By StaffMembers.LastName
Select StaffMembers
Dim total = Empls.Count()

Since we have determined that a LINQ statement produces
an Enumerable list, if you do not need the list itself, you can
declare a variable that is the type returned by a method, put the statement
in parentheses, and then access the method outside the closing parenthesis
using the period operator. Here is an example:

Dim Total = (From StaffMembers
In Employees
Order By StaffMembers.LastName
Select StaffMembers).Count()

Remember that the IEnumerable.Count() method
returns the number of items in the result of the LINQ statement, not the
number of items in the original list. The following examples illustrate it:

These different techniques of calling and using the
Count() method can be applied to most other methods of the Enumerable
class.

An Average of Numbers

If the values you are querying are numbers, you may want
to find their average. To assist you, the Enumerable class
provides a method named Average that is overloaded with a
version for each numeric data type. The syntax for the double type is: