- Get link
- X
- Other Apps
using System;
using System.Collections.Generic;
using System.Data;
public class Program
{
static void Main()
{
List<Person> personList = new List<Person>
{
new Person { Id = 1, Name = "Alice", Age = 30 },
new Person { Id = 2, Name = "Bob", Age = 25 },
new Person { Id = 3, Name = "Charlie", Age = 40 }
};
DataTable dataTable = ConvertListToDataTable(personList);
}
static DataTable ConvertListToDataTable(List<Person> list)
{
DataTable dataTable = new DataTable();
// Assuming the properties in your class are the columns of the DataTable
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Age", typeof(int));
foreach (var item in list)
{
dataTable.Rows.Add(item.Id, item.Name, item.Age);
}
return dataTable;
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
asp.net
Convert List To DataTable
ConvertListToDataTable
dataTable
using System
using System.Collections.Generic
using System.Data
- Get link
- X
- Other Apps
Comments