Daily Archives: November 13, 2020

VB.NET || How To Convert An Array/List/IEnumerable To A DataTable & Convert A DataTable To A List Using VB.NET

The following is a module with functions which demonstrates how to convert an Array/List/IEnumerable to a DataTable, and how to convert a DataTable to a List using VB.NET.

Contents

1. Overview
2. Convert To DataTable - Integer Array
3. Convert To DataTable - List Of Strings
4. Convert To DataTable - List Of Objects
5. Convert To List - Integer DataTable
6. Convert To List - String DataTable
7. Convert To List - Multi-Column DataTable
8. Utils Namespace
9. More Examples


1. Overview

The functions demonstrated on this page are generic extension methods which uses reflection to convert a DataTable to a List(Of T) and an Array/List/IEnumerable to a DataTable.

These functions work on primitive data types, custom structure and class object fields and properties, as well as DBNull.Value, and nullable arrays and lists.

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. Convert To DataTable – Integer Array

The example below demonstrates the use of ‘Utils.Collections.ToDataTable‘ to convert a nullable integer array to a datatable.

When converting nullable types, their value is represented as DBNull in the returned table.


3. Convert To DataTable – List Of Strings

The example below demonstrates the use of ‘Utils.Collections.ToDataTable‘ to convert a list of strings to a datatable.

When converting nullable types, their value is represented as DBNull in the returned table.


4. Convert To DataTable – List Of Objects

The example below demonstrates the use of ‘Utils.Collections.ToDataTable‘ to convert a list of custom objects to a datatable.

A class is used in this example, but a custom structure also works here as well.

When converting nullable types, their value is represented as DBNull in the returned table.


5. Convert To List – Integer DataTable

The example below demonstrates the use of ‘Utils.Collections.ToList‘ to convert a datatable to a List(Of Integer?).

When converting DBNull types, their value is represented as ‘Nothing‘ in the returned list.


6. Convert To List – String DataTable

The example below demonstrates the use of ‘Utils.Collections.ToList‘ to convert a datatable to a List(Of String).

When converting DBNull types, their value is represented as ‘Nothing‘ in the returned list.


7. Convert To List – Multi-Column DataTable

The example below demonstrates the use of ‘Utils.Collections.ToList‘ to convert a datatable to a List(Of Object).

A class is used in this example, but a custom structure also works here as well.

When converting DBNull types, their value is represented as ‘Nothing‘ in the returned list.


8. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


9. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.