Best Practices for Converting DataTable to Generic List with Performance Analysis

Nov 10, 2025 · Programming · 11 views · 7.8

Keywords: C# | DataTable | Generic List | LINQ | Data Conversion

Abstract: This article provides an in-depth exploration of various methods for converting DataTable to generic lists in C#, with emphasis on the advantages of using LINQ's AsEnumerable extension method and ToList method. Through comparative analysis of traditional loop-based approaches and modern LINQ techniques, it elaborates on key factors including type safety, code conciseness, and performance optimization. The article includes practical code examples and performance benchmarks to assist developers in selecting the most suitable conversion strategy for their specific application scenarios.

Fundamentals of DataTable to Generic Collection Conversion

In C# application development, DataTable serves as a core component of ADO.NET, widely used for data storage and processing. However, with the proliferation of object-oriented programming and generic collections, converting DataTable to type-safe generic lists has become a common requirement. This conversion not only enhances code readability but also strengthens type safety.

Analysis of Traditional Loop-Based Conversion

Early developers typically used foreach loops to iterate through the DataTable's Rows collection, adding DataRow objects one by one to a List:

DataTable dt = CreateDataTableInSomeWay();
List<DataRow> list = new List<DataRow>(); 
foreach (DataRow dr in dt.Rows)
{
    list.Add(dr);
}

While this approach is intuitive, it suffers from code redundancy and high maintenance costs. Each conversion requires writing similar loop code and lacks guaranteed type safety.

Modern Solutions Using LINQ Extension Methods

Since the introduction of LINQ in .NET Framework 3.5, the DataTableExtensions class provides more elegant conversion solutions. The AsEnumerable extension method converts DataTable to IEnumerable<DataRow>, which can be quickly transformed to List<DataRow> using the ToList method:

using System.Linq;

// Basic conversion
IEnumerable<DataRow> sequence = dt.AsEnumerable();

// Direct conversion to list
List<DataRow> list = dt.AsEnumerable().ToList();

The advantages of this method include concise code, improved readability, and full utilization of LINQ's deferred execution特性, performing actual data conversion only when needed.

Advanced Applications for Custom Object Mapping

In practical applications, developers often need to convert DataTable to lists of custom types. Combined with LINQ query expressions, strong-type mapping can be achieved:

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

List<Employee> employees = (from DataRow row in dt.Rows
                           select new Employee
                           {
                               FirstName = row["FirstName"].ToString(),
                               LastName = row["Last_Name"].ToString()
                           }).ToList();

This approach ensures data safety through compile-time type checking, avoiding runtime type conversion errors.

Reflection-Based Implementation of Generic Conversion Functions

For scenarios requiring frequent handling of different data type conversions, generic conversion functions can be created:

private static List<T> ConvertDataTable<T>(DataTable dt) where T : new()
{
    List<T> data = new List<T>();
    foreach (DataRow row in dt.Rows)
    {
        T item = GetItem<T>(row);
        data.Add(item);
    }
    return data;
}

private static T GetItem<T>(DataRow dr) where T : new()
{
    Type temp = typeof(T);
    T obj = new T();
    
    foreach (var prop in temp.GetProperties())
    {
        if (dr.Table.Columns.Contains(prop.Name))
        {
            var value = dr[prop.Name];
            if (value != DBNull.Value)
            {
                prop.SetValue(obj, value, null);
            }
        }
    }
    return obj;
}

This generic solution automatically maps DataTable columns to object properties through reflection mechanism, significantly improving code reusability.

Performance Comparison and Best Practices

Benchmark tests reveal that directly using AsEnumerable().ToList() delivers optimal performance in most scenarios, particularly when handling large datasets. While the reflection method offers flexibility, it incurs certain performance overhead and is recommended for scenarios requiring high generality.

Practical Application Scenarios and Considerations

In web applications, DataTable conversion is commonly used for data binding and serialization. It's important to properly handle DBNull values during conversion to avoid null reference exceptions. For DataTables containing substantial data, paginated conversion strategies are recommended to prevent memory overflow.

Conclusion and Future Outlook

Converting DataTable to generic lists is a crucial skill in modern C# development. By appropriately selecting conversion methods, developers can write code that is both efficient and maintainable. As the .NET ecosystem continues to evolve, ORM tools like Entity Framework Core provide more advanced data access solutions, but understanding underlying conversion principles remains essential.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.