CSharp - Distinct for DataTable

Introduction

The Distinct operator removes duplicate rows from a sequence of objects.

Prototypes

public static IEnumerable<T> Distinct<T> (
  this IEnumerable<T> source,
  IEqualityComparer<T> comparer);

In the example, we create a DataTable from an array of Student objects using our common GetDataTable method.

The array will have one duplicate in it.

We then display the DataTable.

Then we remove any duplicate rows by calling the Distinct operator and display the DataTable again.

Demo

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

class Program//from www.  j  av a 2 s  . com
{
    static void Main(string[] args)
    {
        Student[] students = {
          new Student { Id = 1, Name = "Joe Ruby" },
          new Student { Id = 6, Name = "Oracle Express" },
          new Student { Id = 19, Name = "Bob Json" },
          new Student { Id = 45, Name = "CSS Java" },
          new Student { Id = 1, Name = "Joe Ruby" },
          new Student { Id = 12, Name = "Bob Javascript" },
          new Student { Id = 17, Name = "Apache Python" },
          new Student { Id = 32, Name = "Django SQL" }
        };
        DataTable dt = GetDataTable(students);

        Console.WriteLine("{0}Before calling Distinct(){0}",
          System.Environment.NewLine);

        OutputDataTableHeader(dt, 15);

        foreach (DataRow dataRow in dt.Rows)
        {
            Console.WriteLine("{0,-15}{1,-15}",
              dataRow.Field<int>(0),
              dataRow.Field<string>(1));
        }

        IEnumerable<DataRow> distinct =
          dt.AsEnumerable().Distinct(DataRowComparer.Default);

        Console.WriteLine("{0}After calling Distinct(){0}",
          System.Environment.NewLine);

        OutputDataTableHeader(dt, 15);

        foreach (DataRow dataRow in distinct)
        {
            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

Related Topics