
You have a collection or array of elements that you want to filter certain values from. For example, you want to remove null or empty strings from an array in a single method call. The Where extension method in the C# language provides a simple way to accomplish this task.

Let's look at an example program that calls the Where extension method from the System.Linq namespace two times with a different lambda expression each time. In the C# language, lambda expressions use the => operator, which can be read as "goes to", and lambdas are normally parameters to methods that specify a behavior to be used in the method. This program uses Where to filter out null and empty strings in a declarative method call style.
This C# example program uses the Where extension method. It requires System.Linq.
Program that uses Where method [C#]
using System;
using System.Linq;
class Program
{
static void Main()
{
//
// Example array that contains unwanted null and empty strings.
//
string[] array = { "dot", "", "net", null, null, "perls", null };
//
// Use Where method to remove null strings.
//
var result1 = array.Where(item => item != null);
foreach (string value in result1)
{
Console.WriteLine(value);
}
//
// Use Where method to remove null and empty strings.
//
var result2 = array.Where(item => !string.IsNullOrEmpty(item));
foreach (string value in result2)
{
Console.WriteLine(value);
}
}
}
Output
dot -- Begins output from first loop
-- Empty string included
net
perls
dot -- Begins output from second loop
net
perlsDescription. The program calls the Where extension method in the System.Linq namespace twice. There is a string array declared that contains several null and empty strings mixed with the valid strings. The Where extension method is used to eliminate the null and empty strings in a single method call.
The first Where method call specifies that only non-null items should be kept. You can read "item => item != null" as a lambda expression meaning "goes to non-null". The second Where method call simply uses the string static method IsNullOrEmpty and filters on that Boolean value.
Lambda Expression
Let's look at IL Disassembler and peek inside the Where extension method in the System.Linq namespace and System.Core.dll. Internally, Where uses the 'is' operator to search for the best implementation of the logic for the collection type. My opinion is that Where will impose an unacceptable performance penalty for numeric processing but is acceptable for reference collections in most cases. Evaluating the Where method may result in a new sequence being allocated on the managed heap.
IL Disassembler Tutorial
We saw the Where extension method in the C# language and System.Linq namespace. This method provides a clear way to filter the elements in a IEnumerable collection with a single lambda expression. The Where method allows you to write declarative rather than imperative code to do this. The example code shows how to remove empty and null strings from a collection in a single method. If you want to store the Where method result in an array, you can use the ToArray extension method with it.
ToArray Extension Method LINQ Examples