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

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input