Tag Archives: list

C# || How To Convert An Array/List/IEnumerable To A DataTable & Convert A DataTable To A List Using C#

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 C#.

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 struct 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.Extensions.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.Extensions.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.Extensions.ToDataTable‘ to convert a list of custom objects to a datatable.

A class is used in this example, but a custom struct 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.Extensions.ToList‘ to convert a datatable to a List(Of Integer?).

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


6. Convert To List – String DataTable

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

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


7. Convert To List – Multi-Column DataTable

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

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

When converting DBNull types, their value is represented as ‘null‘ 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.

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.

Python || Snippet – How To Input Numbers Into A List & Display Its Contents Back To User

This snippet demonstrates how to place numbers into a list. It also shows how to display the contents of the list back to the user via stdout.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Python Lists
For Loops


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.

Once compiled, you should get this as your output

How many items do you want to place into the list?: 3

Enter item #1: 6
Enter item #2: 4
Enter item #3: 2008

The current items inside the list are:
Item #1: 6
Item #2: 4
Item #3: 2008