Convert DataTable to List in C#: Efficient Data Transformation

Learn how to efficiently convert a C# `DataTable` to a `List` of custom objects. This tutorial provides a clear, step-by-step guide with code examples, explaining how to transform tabular data into a more flexible and object-oriented format for easier processing.



Converting a DataTable to a List in C#

Understanding DataTables and Lists

In C#, a `DataTable` represents a table of data in memory, similar to a database table. It's useful for holding and manipulating tabular data. A `List`, on the other hand, is a dynamic array; it's a more flexible collection that can easily grow or shrink as needed. Sometimes, converting a `DataTable` to a `List` of objects makes it easier to process or display that data.

Why Convert a `DataTable` to a `List`?

Converting a `DataTable` to a `List` of objects can be beneficial for several reasons:

  • LINQ Queries: `Lists` are easier to query using LINQ (Language Integrated Query).
  • Third-Party Libraries: Some libraries expect lists as input.
  • Simplified Data Handling: Working with objects can be more intuitive than working directly with `DataTable` rows.

Steps to Convert a `DataTable` to a `List`

  1. Create a Class: Define a class to represent each row of the `DataTable`. This class should have properties corresponding to the `DataTable`'s columns.
  2. Create a List: Create a new `List` object to store the converted data.
  3. Iterate and Convert: Loop through each row in the `DataTable`. For each row, create an instance of your class, populate its properties with the row's values, and add the object to the list.

Example: Converting a `DataTable` to a `List` of `Employee` Objects

This example demonstrates converting a `DataTable` named "Employee" (with columns "Id", "Name", and "Salary") into a `List` of `Employee` objects. It first defines the `Employee` class and then iterates through the `DataTable` rows using a `foreach` loop.

1. Define the `Employee` Class

C# Code

public class Employee {
    public int Id { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }
}

2. Create and Populate the `DataTable`

C# Code

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Salary", typeof(int));
dt.Rows.Add(1, "John", 5000);
dt.Rows.Add(2, "Mary", 6000);
dt.Rows.Add(3, "Jane", 7000);

3. Convert `DataTable` to `List`

C# Code

List<Employee> employees = new List<Employee>();
foreach (DataRow row in dt.Rows) {
    Employee emp = new Employee();
    emp.Id = Convert.ToInt32(row["Id"]);
    emp.Name = row["Name"].ToString();
    emp.Salary = Convert.ToInt32(row["Salary"]);
    employees.Add(emp);
}

Conclusion

Converting a `DataTable` to a `List` in C# provides a flexible way to work with your data. This technique simplifies data processing using object-oriented approaches, particularly when using LINQ or other methods that work best with lists of objects.