Common Code for the Examples

Virtually every example in this chapter will require a DataTable object on which to perform LINQ to DataSet queries. In real production code, you would typically obtain these DataTable objects by querying a database. However, for some of these examples, I present situations where the data conditions in a typical database table will not suffice. For example, I need duplicate records to demonstrate the Distinct method. Rather than jump through hoops trying to manipulate the database to contain the data I may need, I programmatically create a DataTable containing the specific data I desire for each example. This also relieves you of the burden of having a database for testing the majority of these examples.

Since I will not actually be querying a database for the DataTable objects, and to make creating the DataTable objects easy, I generate them from an array of objects of a predefined class. For the predefined class, I use the Student class.

Example. A Simple Class with Two Public Members
class Student
{
    public int Id;
    public string Name;
}

You should just imagine that I am querying a table named Students where each record is a student, and the table contains two columns: Id and Name.

To make creation of the DataTable simple, and to prevent obscuring the relevant details of each example, I use a common method to convert an array of Student objects into a DataTable object. This allows the data to easily vary from example to example. Here is that common method.

Example. Converting an Array of Student Objects to a DataTable
static DataTable GetDataTable(Student[] students)
{
  DataTable table = new DataTable();

  table.Columns.Add("Id", typeof(Int32));
  table.Columns.Add("Name", typeof(string));

foreach (Student student in students)
  {
    table.Rows.Add(student.Id, student.Name);
  }

  return (table);
}

There isn't anything complex in this method. I just instantiate a DataTable object, add two columns, and add a row for each element in the passed students array.

For many of the examples of the LINQ to DataSet operators, I need to display a DataTable for the results of the code to be clear. While the actual data in the DataTable varies, the code needed to display the DataTable object's header will not. Instead of repeating this code throughout all the examples, I create the following method and call it in any example needing to display a DataTable header.

Example. The OutputDataTableHeader Method
static void OutputDataTableHeader(DataTable dt, int columnWidth)
{
    string format = string.Format("{0}0,-{1}{2}", "{", columnWidth, "}");

    //  Display the column headings.
    foreach(DataColumn column in dt.Columns)
    {
        Console.Write(format, column.ColumnName);
    }
    Console.WriteLine();
    foreach(DataColumn column in dt.Columns)
    {
        for(int i = 0; i < columnWidth; i++)
        {
            Console.Write("=");
        }
    }
    Console.WriteLine();
}

The purpose of the method is to output the header of a DataTable in a tabular form.

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

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