Daily Archives: May 9, 2021

C# || How To Split & Batch An Array/List/IEnumerable Into Smaller Sub-Lists Of N Size Using C#

The following is a module with functions which demonstrates how to split/batch an Array/List/IEnumerable into smaller sublists of n size using C#.

This generic extension function uses a simple for loop to group items into batches.


1. Partition – Integer Array

The example below demonstrates the use of ‘Utils.Extensions.Partition‘ to group an integer array.


2. Partition – String List

The example below demonstrates the use of ‘Utils.Extensions.Partition‘ to group a list of strings.


3. Partition – Custom Object List

The example below demonstrates the use of ‘Utils.Extensions.Partition‘ to group a list of custom objects.


4. Utils Namespace

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


5. 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.

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.

C# || How To Set & Get The Description Of An Enum Using C#

The following is a module with functions which demonstrates how to set and get the description of an enum using C#.

This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.


1. Get Description

The example below demonstrates the use of ‘Utils.Extensions.GetDescription‘ to get the description of enum elements.


2. Utils Namespace

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


3. 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.

C# || How To Iterate & Get The Values Of An Enum Using C#

The following is a module with functions which demonstrates how to iterate and get the values of an enum using C#.

This function is a generic wrapper for the Enum.GetValues function.


1. Get Values – Basic Enum

The example below demonstrates the use of ‘Utils.Methods.GetEnumValues‘ to get the values of a simple Enum.


2. Get Values – Numbered Enum

The example below demonstrates the use of ‘Utils.Methods.GetEnumValues‘ to get the values of a numbered Enum.


3. Utils Namespace

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


4. 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.

C# || How To Resize & Rotate Image, Convert Image To Byte Array, Change Image Format, & Fix Image Orientation Using C#

The following is a module with functions which demonstrates how to resize an image, rotate an image to a specific angle, convert an image to a byte array, change an image format, and fix an image orientation Using C#.

Contents

1. Overview
2. Resize & Rotate - Image
3. Resize & Rotate - Byte Array
4. Resize & Rotate - Memory Stream
5. Convert Image To Byte Array
6. Change Image Format
7. Fix Image Orientation
8. Utils Namespace
9. More Examples


1. Overview

To use the functions in this module, make sure you have a reference to ‘System.Drawing‘ in your project.

One way to do this is, in your Solution Explorer (where all the files are shown with your project), right click the ‘References‘ folder, click ‘Add References‘, then type ‘System.Drawing‘ in the search box, and add a reference to System.Drawing in the results Tab.

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


2. Resize & Rotate – Image

The example below demonstrates the use of ‘Utils.Image.Extensions.Resize‘ and ‘Utils.Image.Extensions.Rotate‘ to resize and rotate an image object.

Resizing and rotating an image returns a newly created image object.


3. Resize & Rotate – Byte Array

The example below demonstrates the use of ‘Utils.Image.Extensions.Resize‘ and ‘Utils.Image.Extensions.Rotate‘ to resize and rotate a byte array.

Resizing and rotating an image returns a newly created image object.


4. Resize & Rotate – Memory Stream

The example below demonstrates the use of ‘Utils.Image.Resize‘ and ‘Utils.Image.Rotate‘ to resize and rotate a memory stream.

Resizing and rotating an image returns a newly created image object.


5. Convert Image To Byte Array

The example below demonstrates the use of ‘Utils.Image.Extensions.ToArray‘ to convert an image object to a byte array.

The optional function parameter allows you to specify the image format.


6. Change Image Format

The example below demonstrates the use of ‘Utils.Image.Extensions.ConvertFormat‘ to convert the image object raw format to another format.

Converting an images format returns a newly created image object.


7. Fix Image Orientation

The example below demonstrates the use of ‘Utils.Image.Extensions.FixOrientation‘ to update the image object to reflect its Exif orientation.

When you view an image in a Photo Viewer, it automatically corrects image orientation if it has Exif orientation data.

Utils.Image.Extensions.FixOrientation‘ makes it so the loaded image object reflects the correct orientation when rendered according to its Exif orientation data.


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.