Monday, 13 February 2012

LINQ Oprations with Enumerable Class

LINQ and the Enumerable Class


Introduction to the Enumerable Class
 
Description


In our introduction to LINQ, we saw how to create a LINQ statement by declaring a variable using the var keyword. The var 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 var keyword, you can declare the statement using that interface. Inside the <> operator, enter the type of list that the collection variable contains. Here is an example:


using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var employees = new Employee[]
        {
            new Employee(971974, "Patricia", "Katts", 24.68M),
            new Employee(208411, "Raymond", "Kouma", 20.15M),
            new Employee(279374, "Hél�ne", "Mukoko", 15.55M),
            new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
            new Employee(971394, "Gertrude", "Monay", 20.55M)
        };

        IEnumerable<Employee> empls = from staffMembers
                                      in employees
                                      select staffMembers;

        Console.WriteLine("+========+============+===========+========+");
        Console.WriteLine("| Empl # | First Name | Last Name | Salary |");
        foreach (var staff in empls)
        {
            Console.WriteLine("+--------+------------+-----------+--------+");
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", staff.EmployeeNumber,
                staff.FirstName, staff.LastName, staff.HourlySalary);
        }
        Console.WriteLine("+=======+============+===========+=========+");

        Console.WriteLine();
        return 0;
    }
}

public class Employee
{
    public int EmployeeNumber;
    public string FirstName;
    public string LastName;
    public decimal HourlySalary;

    public Employee(int number = 0,
                       string firstName = "John",
                       string lastName = "Doe",
                       decimal salary = 0M)
    {
        EmployeeNumber = number;
        FirstName = firstName;
        LastName = lastName;
        HourlySalary = salary;
    }

    internal string GetFullName()
    {
        return LastName + ", " + FirstName;
    }
}


This would produce:

IEnumerable


Returning an Enumerable


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:


using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    static IEnumerable<double> GetQuery(double[] list)
    {
        return from n in list select n;
    }

    public static int Main()
    {
        var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

        var number = GetQuery(numbers);

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}


This would produce:

IEnumerable



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: 

public static IEnumerable<TResult> Select<TSource, TResult>(
 this IEnumerable<TSource> source,
 Func<TSource, TResult> selector
);
 
 public static IEnumerable<TResult> Select<TSource, TResult>(
 this IEnumerable<TSource> source,
 Func<TSource, int, TResult> selector
);
 


This method expects a variable that would hold the result of the selection. You can pass it a lambda expression. This can be done as follows:

IEnumerable<Employee> empls = 
 employees.Select<Employee, Employee>(staffMembers => staffMembers);
 

You don't have to specify the TSource and the TResult classes:

 
IEnumerable<Employee> empls = employees.Select(staffMembers => staffMembers);


After doing this, the IEnumerable variable can be used in a foreach loop. 

Here is an example:

public class Exercise
{
    public static int Main()
    {
        var employees = new Employee[]
        {
            new Employee(971974, "Patricia", "Katts", 24.68M),
            new Employee(208411, "Raymond", "Kouma", 20.15M),
            new Employee(279374, "Hél�ne", "Mukoko", 15.55M),
            new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
            new Employee(971394, "Gertrude", "Monay", 20.55M)
        };

        IEnumerable<Employee> empls = employees.Select(staffMembers => staffMembers);

        Console.WriteLine("+========+============+===========+========+");
        Console.WriteLine("| Empl # | First Name | Last Name | Salary |");
        foreach (var staff in empls)
        {
            Console.WriteLine("+--------+------------+-----------+--------+");
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", staff.EmployeeNumber,
                staff.FirstName, staff.LastName, staff.HourlySalary);
        }
        Console.WriteLine("+=======+============+===========+=========+");

        Console.WriteLine();
        return 0;
    }
}


Where is the Enumerable?



To support conditions of a query, the Enumerable class is equipped with a method named Where that is overloaded with two versions. 

The syntax of one is:

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
        Func<TSource, bool> predicate);



Here is an example of calling it:

public class Exercise
{
    public static int Main()
    {
        var numbers = new double[]{ 637.58, 48.5, 12.44, 525.38,
                                    45.3, 6.28, 2448.32, 632.04 };

        var lstNumbers = from n
                         in numbers
                         select n;

        foreach (var number in lstNumbers)
            Console.WriteLine(number.ToString());

        Console.WriteLine("=+=+=+=+=+=+=+");
        var lstWhere = (from n
                        in numbers
                        select n).Where(high => high > 500);

        foreach (var where in lstWhere)
            Console.WriteLine(where.ToString());

        Console.WriteLine();
        return 0;
    }
}


This would produce:

IEnumerable


Sorting a List


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:

 
IEnumerable<Employee> empls = from staffMembers
                              in employees
                              orderby staffMembers.LastName
                              select staffMembers;
 
 

To support the ability to sort the values, the IEnumerable interface provides its own OrderBy() method that is overloaded with two versions. 

Their syntaxes are:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
 this IEnumerable<TSource> source,
 Func<TSource, TKey> keySelector
);
 
 public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
 this IEnumerable<TSource> source,
 Func<TSource, TKey> keySelector,
 IComparer<TKey> comparer
);
 
 

Here is an example:


public class Exercise
{
    public static int Main()
    {
        var employees = new Employee[]
        {
            new Employee(971974, "Patricia", "Katts", 24.68M),
            new Employee(208411, "Raymond", "Kouma", 20.15M),
            new Employee(279374, "Hél�ne", "Mukoko", 15.55M),
            new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
            new Employee(971394, "Gertrude", "Monay", 20.55M)
        };

        IEnumerable<Employee> empls = employees.OrderBy(staffMembers => staffMembers.LastName);

        Console.WriteLine("+========+============+===========+========+");
        Console.WriteLine("| Empl # | First Name | Last Name | Salary |");
        foreach (var staff in empls)
        {
            Console.WriteLine("+--------+------------+-----------+--------+");
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", staff.EmployeeNumber,
                staff.FirstName, staff.LastName, staff.HourlySalary);
        }
        Console.WriteLine("+=======+============+===========+=========+");

        Console.WriteLine();
        return 0;
    }
}
 

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 var 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:

using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new int[] { 12, 44, 525, 38, 6, 28, 2448, 32, 632, 04 };

        var number = from n in numbers select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}


This would produce:

Numbers



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:

public static IEnumerable<int> Range(int start, int count);
 

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:

public class Exercise
{
    public static int Main()
    {
        IEnumerable<int> range = Enumerable.Range(22, 8);

        var number = from n in range select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}
 

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

Numbers


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

public class Exercise
{
    public static int Main()
    {
        var range = Enumerable.Range(96, 10);

        var number = from n
                     in range
                     where n % 2 == 0
                     select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}


This would produce:

Numbers


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:


public class Exercise
{
    public static int Main()
    {
        var employees = new Employee[]
     {
            new Employee(971974, "Patricia", "Katts", 24.68M),
            new Employee(208411, "Raymond", "Kouma", 20.15M),
            new Employee(279374, "Hél�ne", "Mukoko", 15.55M),
            new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
            new Employee(971394, "Gertrude", "Monay", 20.55M)
     };

        IEnumerable<Employee> empls = from staffMembers
                            in employees
                            orderby staffMembers.LastName
                            select staffMembers;

        Console.WriteLine("+========+============+===========+========+");
        Console.WriteLine("| Empl # | First Name | Last Name | Salary |");
        foreach (var staff in empls)
        {
            Console.WriteLine("+--------+------------+-----------+--------+");
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", staff.EmployeeNumber,
                staff.FirstName, staff.LastName, staff.HourlySalary);
        }
        Console.WriteLine("+=======+============+===========+=========+");

        Console.WriteLine("\nNumber of Employees: {0}", empls.Count());
        Console.WriteLine();
        return 0;
    }
}


This would produce:

Numbers


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

Here are examples:

IEnumerable<Employee> empls = from staffMembers
                         in employees
                         orderby staffMembers.LastName
                         select staffMembers;
var 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:

 
var total = (from staffMembers
             in employees
             orderby 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: 


 IEnumerable<Student> pupils = from studs
                              in students
                              select studs;
int boys = (from males
            in pupils
            where males.Gender == Genders.Male
            select males).Count();

int girls = (from females
             in pupils
             where ((females.Gender != Genders.Male) && 
      (females.Gender != Genders.Unknown))
             select females).Count();



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:

public static double Average(this IEnumerable source);


Here is an example of calling this method:

public class Exercise
{
    public static int Main()
    {
        var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

        var number = from n
                     in numbers
                     select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine("---------------------");
        Console.WriteLine("Average: {0}", number.Average());

        Console.WriteLine();
        return 0;
    }
}


This would produce: 

Average



If it is only the average that you want, you can include the LINQ statement in parentheses and call the Average method outside. Here is an example:


var average = (from n
               in numbers
               select n).Average();

Console.WriteLine("{0}", average);


Of course, you can add a where condition if you want to restrict the result.

No comments :