CSharp - Union for DataRow

Introduction

The Union operator produces a sequence of DataRow objects that is the union of two sequences of DataRow objects.

Prototypes

public static IEnumerable<T> Union<T> (
        this IEnumerable<T> first,
        IEnumerable<T> second,
        IEqualityComparer<T> comparer);

Demo

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

class Program//from  w w  w.  j  a  va  2 s. c  om
{
    static void Main(string[] args)
    {
        Student[] students = {
        new Student { Id = 1, Name = "Joe Ruby" },
        new Student { Id = 7, Name = "Apache Python" },
        new Student { Id = 13, Name = "Scala Sinclair" },
        new Student { Id = 72, Name = "Django SQL" }
      };

        Student[] students2 = {
        new Student { Id = 5, Name = "Abe Html" },
        new Student { Id = 7, Name = "Apache Python" },
        new Student { Id = 29, Name = "Future Man" },
        new Student { Id = 72, Name = "Django SQL" }
      };

        DataTable dt1 = GetDataTable(students);
        IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
        DataTable dt2 = GetDataTable(students2);
        IEnumerable<DataRow> seq2 = dt2.AsEnumerable();

        IEnumerable<DataRow> union =
          seq1.Union(seq2, System.Data.DataRowComparer.Default);

        Console.WriteLine("{0}Results of Union() with comparer{0}",
          System.Environment.NewLine);

        OutputDataTableHeader(dt1, 15);

        foreach (DataRow dataRow in union)
        {
            Console.WriteLine("{0,-15}{1,-15}",
              dataRow.Field<int>(0),
              dataRow.Field<string>(1));
        }
        union = seq1.Union(seq2);

        Console.WriteLine("{0}Results of Union() without comparer{0}",
          System.Environment.NewLine);

        OutputDataTableHeader(dt1, 15);

        foreach (DataRow dataRow in union)
        {
            Console.WriteLine("{0,-15}{1,-15}",
              dataRow.Field<int>(0),
              dataRow.Field<string>(1));
        }
    }
    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();
    }
    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);
    }
}

class Student
{
    public int Id;
    public string Name;
}

Result