Donate

How To Convert DataTable To List Using LINQ And C#

Hello,

The code below converts a DataTable object to generic List object using LINQ and C#. It assumes that the DataTable columns match with class properties.
C# Code
1
2
3
4
5
6
7
8
var personEnumerable = table.AsEnumerable();
List<Person> ListPosition = new List<Person>();
ListPosition = (from item in personEnumerable
            select new Person
            {
                ID = item.Field<int>("ID").ToString(),
                Salary = item.Field<double>("Salary").ToString()
            }).ToList();
Class
1
2
3
4
5
public class Person
{
    public string ID { get; set; }
    public string Salary { get; set; }
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid