Introduction
Data manipulation and conversion are common tasks in software development, especially when dealing with heterogeneous data sources. In C#/.NET, converting data from one format to another, such as from a list of custom objects to a DataTable, is a frequent requirement. In this article, we'll explore efficient techniques for converting data from a list to a DataTable and vice versa, along with practical examples to illustrate each conversion process.
Converting a List of Custom Objects to a DataTable
The first scenario we'll cover is converting a list of custom objects to a DataTable. This is useful when you have data stored in memory as objects and need to transform it into a tabular format for further processing or display. Here's how you can achieve this:
using System;
using System.Collections.Generic;
using System.Data;
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
// Sample list of custom objects
List<Person> personList = new List<Person>
{
new Person { ID = 1, Name = "John", Age = 30 },
new Person { ID = 2, Name = "Alice", Age = 25 },
new Person { ID = 3, Name = "Bob", Age = 35 }
};
// Convert list to DataTable
DataTable dataTable = ConvertListToDataTable(personList);
// Convert DataTable to List<DataRow>
List<DataRow> dataList = ConvertDataTableToList(sourceDataTable);
// Display the converted DataTable
foreach (DataRow row in dataTable.Rows)
{
foreach (var item in row.ItemArray)
{
Console.Write($"{item}\t");
}
Console.WriteLine();
}
}
// Method to convert list of custom objects to DataTable
private static DataTable ConvertListToDataTable(List<Person> personList)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Age", typeof(int));
foreach (var person in personList)
{
dataTable.Rows.Add(person.ID, person.Name, person.Age);
}
return dataTable;
}
// Method to convert DataTable to List<DataRow>
private static List<DataRow> ConvertDataTableToList(DataTable dataTable)
{
List<DataRow> dataList = new List<DataRow>();
foreach (DataRow row in dataTable.Rows)
{
dataList.Add(row);
}
return dataList;
}
}
In this example, we define a Person class representing the structure of a person with properties ID, Name, and Age. We then create a list of Person objects (personList) and convert it to a DataTable (dataTable) using the ConvertListToDataTable method.
Transferring Values from One DataTable to Another
Another common scenario is transferring values from one DataTable to another DataTable. This can be useful when you have data in a source DataTable and need to transform it or copy it to a destination DataTable. Here's how you can accomplish this:
using System;
using System.Collections.Generic;
using System.Data;
public class Program
{
public static void Main(string[] args)
{
// Sample DataTable with source data
DataTable sourceDataTable = GetSourceDataTable();
// Sample DataTable with destination structure
DataTable destinationDataTable = GetDestinationDataTable();
// Transfer values from source to destination DataTable
TransferValues(sourceDataTable, destinationDataTable);
// Display the destination DataTable
Console.WriteLine("Destination DataTable:");
foreach (DataRow row in destinationDataTable.Rows)
{
foreach (var item in row.ItemArray)
{
Console.Write($"{item}\t");
}
Console.WriteLine();
}
}
// Method to create a sample source DataTable
private static DataTable GetSourceDataTable()
{
DataTable sourceDataTable = new DataTable();
sourceDataTable.Columns.Add("ID", typeof(int));
sourceDataTable.Columns.Add("Name", typeof(string));
sourceDataTable.Columns.Add("Age", typeof(int));
sourceDataTable.Rows.Add(1, "John", 30);
sourceDataTable.Rows.Add(2, "Alice", 25);
sourceDataTable.Rows.Add(3, "Bob", 35);
return sourceDataTable;
}
// Method to create a sample destination DataTable with the same structure as the source DataTable
private static DataTable GetDestinationDataTable()
{
DataTable destinationDataTable = new DataTable();
destinationDataTable.Columns.Add("ID", typeof(int));
destinationDataTable.Columns.Add("Name", typeof(string));
destinationDataTable.Columns.Add("Age", typeof(int));
return destinationDataTable;
}
// Method to transfer values from source DataTable to destination DataTable
private static void TransferValues(DataTable sourceDataTable, DataTable destinationDataTable)
{
foreach (DataRow sourceRow in sourceDataTable.Rows)
{
DataRow newRow = destinationDataTable.NewRow();
newRow["ID"] = sourceRow["ID"];
newRow["Name"] = sourceRow["Name"];
newRow["Age"] = sourceRow["Age"];
destinationDataTable.Rows.Add(newRow);
}
}
}
In this example, we have a source DataTable (sourceDataTable) with sample data and a destination DataTable (destinationDataTable) with the same structure as the source DataTable. We then transfer values from the source DataTable to the destination DataTable using the TransferValues method.
Conclusion
Efficient data conversion is essential for building robust and scalable applications. In this article, we've explored techniques for converting data from a list of custom objects to a DataTable and transferring values between DataTables in C#/.NET. By understanding and applying