DateTimeExtensions

ConvertDateToNumeric

This method converts DateTime to a purely numeric value in the format "yyyyMMdd". Example: 20240112

var dt = DateTime.Now;
var myInt = dt.ConvertDateToNumeric(dt);

ConvertDateTimeToString

This method converts the DateTime object to the format "yyyy-MM-dd HH:mm:ssZ"

var dt = DateTime.Now;
var myInt = dt.ConvertDateTimeToString(dt);

IsBetween

This method checks if a given DateTime is between two other DateTime values. Returns true if the date is between the start and end dates, inclusive.

var date = new DateTime(2023, 10, 15);
var startDate = new DateTime(2023, 10, 1);
var endDate = new DateTime(2023, 10, 31);

var result = date.IsBetween(startDate, endDate);

StartOfDay

This method returns the start of the day for a given DateTime, setting the time to 00:00:00.

var date = new DateTime(2023, 10, 15, 14, 30, 45);     
var result = date.StartOfDay();

EndOfDay

This method returns the end of the day for a given DateTime, setting the time to 23:59:59.

var date = new DateTime(2023, 10, 15, 14, 30, 45);      
var result = date.EndOfDay();

IsWeekend

This method checks if a given DateTime falls on a weekend (Saturday or Sunday).

var date = new DateTime(2023, 10, 14); // Saturday
var result = date.IsWeekend();

NextBusinessDay

This method calculates the next business day after a given DateTime, skipping weekends.

var date = new DateTime(2023, 10, 13); // Friday      
var result = date.NextBusinessDay();

IEnumerableExtensions

IsEmpty

This method checks, if a given IEnumerable is empty.

var filledEnumeration = new IEnumeration<Model>();
filledEnumeration.IsEmpty<Model>(); // returns true or false

IsNotEmpty

This method checks, if a given IEnumerable is not empty.

var filledEnumeration = new IEnumeration<Model>();
filledEnumeration.IsNotEmpty<Model>(); // returns true or false

IsNullOrEmpty

This method checks if a given IEnumerable is null or empty.

IEnumerable<int>? collection = null;
var result = collection.IsNullOrEmpty();

HasItems

This method checks if a given IEnumerable has any items.

var collection = new List<int> { 1, 2, 3 };
var result = collection.HasItems();

Foreach

This method iterates over each item in an IEnumerable and executes a specified action on each item.

var collection = new List<int> { 1, 2, 3 };      
var result = collection.HasItems();

WhereNotNull

This method filters an IEnumerable to include only non-null items.

var collection = new List<string?> { "a", null, "b", null, "c" };      
var result = collection.WhereNotNull();

ToSafeList

This method converts an IEnumerable to a List, ensuring that it is not null. If the input is null, it returns an empty list.

IEnumerable<int>? collection = null;      
var result = collection.ToSafeList();

OptimisedLinqExtensions

AnyFast

This method checks if any element in an IEnumerable satisfies a specified condition, optimized for performance.

var collection = new List<int> { 1, 2, 3 };      
var result = collection.AnyFast();

FirstOrDefault

This method retrieves the first element of an IEnumerable that satisfies a specified condition, or returns a default value if no such element exists. It is optimized for performance.

var collection = new List<int>();
var result = collection.FirstOrDefault(0);

TakeFast

This method retrieves a specified number of elements from the start of an IEnumerable, optimized for performance.

var collection = new List<int> { 1, 2, 3 };
var result = collection.TakeFast(0);

String Extensions

GetSalutationText

This method returns the salutation based on a gender characteristic.

"Male" becomes "Herr" and "Female" becomes "Frau" (german words).

var gender = "Male"
var salutation = gender.GetSalutationText();

ReturnGenderId

This method returns a gender ID based on a gender characteristic.

"Male" becomes "1" and "Female" becomes "2."

var gender = "Male"
var genderId = gender.ReturnGenderId();

IsNullOrEmpty

This method checks if a given string is null or empty.

var value = "Test";
var result = value.IsNullOrEmpty();

ToSafeString

This method converts a string to a safe string, returning an empty string if the input is null.

object? value = null;
var result = value.ToSafeString();

Truncate

This method truncates a string to a specified length, appending an ellipsis ("...") if the string exceeds that length.

var value = "This is a long string";
var maxLength = 10;
var result = value.Truncate(maxLength);

ValidationExtensions

IsValidEmail

This method checks if a given string is a valid email address using System.Net.Mail.

var email = "test@example.com";
var result = email.IsValidEmail();

IsValidPhoneNumber

This method checks if a given string is a valid phone number. Number must between 10 and 15 digits long.

var phoneNumber = "+1234567890";
var result = phoneNumber.IsValidPhoneNumber();

IsInRange

This method checks if a given integer is within a specified range. Can be usedwith int and decimal types.

var value = 15;
var result = value.IsInRange(1, 10);

ValidateRequired

This method checks if a given string is not null or empty, and throws an exception if it is.

var value = "Test";
var fieldName = "Field";
var result = value.ValidateRequired(fieldName);