Skip to content

Enumerable Extensions

StrutTower edited this page Nov 18, 2023 · 1 revision

Enumerable Extensions

IEnumerable extension methods

SafeAny()

Functions basically the same as Linq .Any() but returns null instead of throwing an error if the enumerable object is null

List<Person> people = GetAllPeople();

bool hasAny = people.SafeAny();
bool hasAnyMatching = people.SafeAny(x => x.LastName == "Smith");

List<Person> nullList = null;
bool nullAny = nullList.SafeAny();
// returns false instead of throwing an error

IsOfType()

Checks if the generic type of the enumerable object matches the supplied type

List<Person> people = new List<Person>();
bool isPerson = people.IsOfType(typeof(Person));
// returns true

bool isOrder = people.IsOfType(typeof(Order));
// returns false

Batch()

Splits a enumerable into smaller list based on the batch size supplied.

List<Person> people = GetAllPeople();
IEnumerable<IEnumerable<Person>> batches = people.Batch(50);

Clone this wiki locally