C# linq转datatable
时间: 2024-02-05 17:05:11 浏览: 154
以下是将一个List转换为DataTable的C# Linq代码示例:
```csharp
List<Person> personList = new List<Person>()
{
new Person() { Id = 1, Name = "Alice", Age = 20 },
new Person() { Id = 2, Name = "Bob", Age = 25 },
new Person() { Id = 3, Name = "Charlie", Age = 30 }
};
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Age", typeof(int));
personList.ForEach(person => dataTable.Rows.Add(person.Id, person.Name, person.Age));
```
其中,Person是一个自定义的类,用于存储每个人的信息,如下所示:
```csharp
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
```
阅读全文