M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Database.Any(), None(), Count()

In this lesson, we will learn about very useful extension method on IEnumerable type.

Any()

This method has two overloads and is used with conditional statements where we need to check if the resulting sequence is not empty

Overload 1

The first overload of this method doesn’t require any criteria / condition and is used just to determine if a sequence holds any elements, as shown below:

Overload 2

The second overload requires a condition statement and determines if any element of a sequence satisfies the condition. The supplied condition can contain any number of logical operators.

None()

This method has also two overloads and is also used with conditional statements where we need to check if the resulting sequence is empty.

Overload 1

The first overload of this method doesn’t require any criteria / conditions and is used just to determine if sequence holds no elements, as shown below:

Overload 2

The second overload requires a condition statement and determines if a sequence contains no elements which satisfy the condition. The supplied condition can contain any number of logical operators.

Count()

This method is used to determine the number of elements in any sequence and has two overloads.

Important: This method is often implemented instead of the previous two methods mentioned above, by using the returned integer to determine if a sequence contains any elements or not. This is a bad practice as the Count() method returns all the elements of a sequence and can be memory intensive, the previously mentioned Any() and None() methods should be used in those scenarios instead. This method should only be used to determine the number of elements in any sequence.

Overload 1

This overload does not require any condition or parameter and simply returns the number of elements in the sequence. This method can be used in conditional statements to determine if sequence contains a specific number of elements.

In the above example, we used count in a conditional statement to determine if the Employee sequence contains more than 5 elements.

Note: In above example, we cannot use "Database.Any()" method because that will only determine the existence of elements in a sequence but not the specific number of elements we require.

Overload 2

The second overload has two parameters, first is mandatory and requires a predicate function or condition and second parameter is type of "Query Option" (M#’s internal class) and is used to pass additional query options e.g. sorting, ranging etc. The second parameter is optional.

Count Method with String Return Type

M# provides two very useful methods, which return the total number of elements in a sequence with a "Title" and it determines the singular and plural representation of given title based on the number of elements. This method has two overloads, explained below:

Overload 1

This overload returns a "String" and is very useful in cases where you want to display the number of sequence with the object title e.g. "5 Employees, 10 Users".

The method above will return a singular "Title text (in our case it’s Employee)" with a prefix of "Total numbers of elements" (e.g. the above code will returns "0 Employee" or "1 Employee" if the sequence contains one or less than element one element). The method returns Plural "Title" when sequence contains more than one element e.g. "5 Employees"

Overload 2

The second overload requires one additional parameter "ZeroQualifier" of type string. The difference between this and the 1st overload is that this overload will return the provided "ZeroQualifier" when a sequence has less than one element.

The above code will return "No Employee" if sequence contains no elements, else it will return the same result as overload one.