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 DateTime BirthDate { get; set; }
    public decimal Salary { get; set; }
    public bool IsActive { get; set; }
}

class Program
{
    static void Main()
    {
        // 1. 创建测试数据 List
        List<Person> peopleList = new List<Person>
        {
            new Person { Id = 1, Name = "张三", Age = 30, BirthDate = new DateTime(1993, 5, 15), Salary = 5000.50m, IsActive = true },
            new Person { Id = 2, Name = "李四", Age = 25, BirthDate = new DateTime(1998, 8, 20), Salary = 4500.75m, IsActive = true },
            new Person { Id = 3, Name = "王五", Age = 35, BirthDate = new DateTime(1988, 3, 10), Salary = 6000.00m, IsActive = false }
        };

        // 2. 将 List 转换为 DataTable
        DataTable peopleTable = ConvertListToDataTable(peopleList);

        // 3. 显示结果
        DisplayDataTable(peopleTable);
    }

    /// <summary>
    /// 使用手动映射将 List<Person> 转换为 DataTable
    /// </summary>
    public static DataTable ConvertListToDataTable(List<Person> peopleList)
    {
        // 创建 DataTable
        DataTable dataTable = new DataTable("People");
        
        // 手动创建列(定义表结构)
        dataTable.Columns.Add("ID", typeof(int));
        dataTable.Columns.Add("姓名", typeof(string));
        dataTable.Columns.Add("年龄", typeof(int));
        dataTable.Columns.Add("生日", typeof(DateTime));
        dataTable.Columns.Add("薪资", typeof(decimal));
        dataTable.Columns.Add("是否激活", typeof(bool));

        // 如果传入的列表为空或null,返回空表
        if (peopleList == null || peopleList.Count == 0)
            return dataTable;

        // 手动映射每一行数据
        foreach (Person person in peopleList)
        {
            // 创建新行
            DataRow row = dataTable.NewRow();
            
            // 手动映射每个属性到对应的列
            row["ID"] = person.Id;
            row["姓名"] = person.Name;
            row["年龄"] = person.Age;
            row["生日"] = person.BirthDate;
            row["薪资"] = person.Salary;
            row["是否激活"] = person.IsActive;

            // 将行添加到表中
            dataTable.Rows.Add(row);
        }

        return dataTable;
    }

    /// <summary>
    /// 显示 DataTable 的内容(用于验证)
    /// </summary>
    public static void DisplayDataTable(DataTable dataTable)
    {
        Console.WriteLine("DataTable 内容:");
        Console.WriteLine("=========================================");
        
        // 显示列头
        foreach (DataColumn column in dataTable.Columns)
        {
            Console.Write($"{column.ColumnName,-10} ");
        }
        Console.WriteLine();
        Console.WriteLine(new string('-', 70));

        // 显示数据行
        foreach (DataRow row in dataTable.Rows)
        {
            Console.WriteLine($"{row["ID"],-10} {row["姓名"],-10} {row["年龄"],-10} {((DateTime)row["生日"]).ToString("yyyy-MM-dd"),-12} {row["薪资"],-10} {row["是否激活"],-10}");
        }
    }
}

更多推荐