C# - Convert DataSet to List

How to convert dataset to class object list using c#.net in .net core. Here i have used .net core 10 console application and linq to get list.
In today's article I will show you a simple tutorial with example how you can convert the dataset value to a class object list in c#.net and vb.net. Here in current example first i have shown how to add value to dataset and then convert the dataset to list.

This example you can use in .net core. Here I have used .net core 10 console application with visual studio 2026. So, for this article first we will create a new .net core console application with c#.net and vb.net. Now add a class file and add the below code.

C#.Net

public class EmployeeDetail
{
    public int EmpId { get; set; }
    public string EmpName { get; set; } = string.Empty;
    public string Project { get; set; } = string.Empty;
} 
Now let's add the code to prepare the dataset and convert it into class object list.
using System.Data;

DataSet dataSet = LoadDataSet();
List<EmployeeDetail> employeeDetails = new List<EmployeeDetail>();
employeeDetails = dataSet.Tables[0].AsEnumerable()
                   .Select(dataRow => new EmployeeDetail
                   {
                       EmpId = dataRow.Field<int>("EmpId"),
                       EmpName = dataRow.Field<string>("EmpName"),
                       Project = dataRow.Field<string>("Project")
                   }).ToList();
Console.WriteLine("EMPLOYEE DETAIL LIST");
Console.WriteLine("---------------------");
foreach (var emp in employeeDetails)
{
    Console.WriteLine($"EmpId: {emp.EmpId}, EmpName: {emp.EmpName}, Project: {emp.Project}");
}

// Method to load DataSet with sample data
DataSet LoadDataSet()
{
    // Create a new DataSet
    DataSet dataSet = new DataSet();

    // Create a new DataTable
    DataTable dataTable = new DataTable("EmployeeTable");
    dataTable.Columns.Add(new DataColumn("EmpId", typeof(int)));
    dataTable.Columns.Add(new DataColumn("EmpName", typeof(string)));
    dataTable.Columns.Add(new DataColumn("Project", typeof(string)));

    // Add a new DataRow
    DataRow dataRow1 = dataTable.NewRow();
    dataRow1["EmpId"] = 101;
    dataRow1["EmpName"] = "John";
    dataRow1["Project"] = "Project A";
    dataTable.Rows.Add(dataRow1);

    DataRow dataRow2 = dataTable.NewRow();
    dataRow2["EmpId"] = 102;
    dataRow2["EmpName"] = "Pranav";
    dataRow2["Project"] = "Project B";
    dataTable.Rows.Add(dataRow2);

    DataRow dataRow3 = dataTable.NewRow();
    dataRow3["EmpId"] = 103;
    dataRow3["EmpName"] = "Stel";
    dataRow3["Project"] = "Project C";
    dataTable.Rows.Add(dataRow3);

    // Add the DataTable to the DataSet
    dataSet.Tables.Add(dataTable);

    return dataSet;
} 
In above code I am using a console application. Here I have created a method named as LosdDataset() which we use to load the data in dataset. In main method i have read the data and then converted the dataset to list and displayed the list value.

VB.Net

Public Class EmployeeDetail
    Public Property EmpId As Integer
    Public Property EmpName As String = String.Empty
    Public Property Project As String = String.Empty
End Class 
Now let's add the code to prepare the dataset and convert it into class object list.
Imports System
Imports System.Data

Module Program
    Sub Main(args As String())
        Dim dataSet As DataSet = LoadDataSet()
        Dim employeeDetails As List(Of EmployeeDetail) = New List(Of EmployeeDetail)()
        employeeDetails = dataSet.Tables(0).AsEnumerable().[Select](Function(dataRow) New EmployeeDetail With {
.EmpId = dataRow.Field(Of Integer)("EmpId"),
.EmpName = dataRow.Field(Of String)("EmpName"),
.Project = dataRow.Field(Of String)("Project")
}).ToList()
        Console.WriteLine("EMPLOYEE DETAIL LIST")
        Console.WriteLine("---------------------")

        For Each emp In employeeDetails
            Console.WriteLine($"EmpId: {emp.EmpId}, EmpName: {emp.EmpName}, Project: {emp.Project}")
        Next
End Sub
' ----Method to load DataSet with sample data----
Private Function LoadDataSet() As DataSet
        ' Create a new DataSet
        Dim dataSet As DataSet = New DataSet()

        ' Create a new DataTable
        Dim dataTable As DataTable = New DataTable("EmployeeTable")
        dataTable.Columns.Add(New DataColumn("EmpId", GetType(Integer)))
        dataTable.Columns.Add(New DataColumn("EmpName", GetType(String)))
        dataTable.Columns.Add(New DataColumn("Project", GetType(String)))

        ' Add a new DataRow
        Dim dataRow1 As DataRow = dataTable.NewRow()
        dataRow1("EmpId") = 101
        dataRow1("EmpName") = "John"
        dataRow1("Project") = "Project A"
        dataTable.Rows.Add(dataRow1)

        Dim dataRow2 As DataRow = dataTable.NewRow()
        dataRow2("EmpId") = 102
        dataRow2("EmpName") = "Pranav"
        dataRow2("Project") = "Project B"
        dataTable.Rows.Add(dataRow2)

        Dim dataRow3 As DataRow = dataTable.NewRow()
        dataRow3("EmpId") = 103
        dataRow3("EmpName") = "Stel"
        dataRow3("Project") = "Project C"
        dataTable.Rows.Add(dataRow3)

        ' Add the DataTable to the DataSet
        dataSet.Tables.Add(dataTable)

        Return dataSet
    End Function
End Module 
Now let's run the code to check the output.

Employee List

Post a Comment