Queryable.DefaultIfEmpty Method

Definition

Returns the elements in a sequence or a default valued singleton collection if the sequence is empty.

Overloads

DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.

DefaultIfEmpty<TSource>(IQueryable<TSource>)

Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.

DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.

C#
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource>(this System.Linq.IQueryable<TSource> source, TSource defaultValue);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

The IQueryable<T> to return the specified value for if empty.

defaultValue
TSource

The value to return if the sequence is empty.

Returns

IQueryable<TSource>

An IQueryable<T> that contains defaultValue if source is empty; otherwise, source.

Exceptions

source is null.

Examples

The following code example shows a situation in which it is useful to call DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) in a LINQ query. A default value is passed to DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) in this example.

C#
// Create a list of Pet objects.
List<Pet> pets =
    new List<Pet>{ new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

// This query selects only those pets that are 10 or older.
// In case there are no pets that meet that criteria, call
// DefaultIfEmpty(). This code passes an (optional) default
// value to DefaultIfEmpty().
string[] oldPets =
    pets.AsQueryable()
    .Where(pet => pet.Age >= 10)
    .Select(pet => pet.Name)
    .DefaultIfEmpty("[EMPTY]")
    .ToArray();

Console.WriteLine("First query: " + oldPets[0]);

// This query selects only those pets that are 10 or older.
// This code does not call DefaultIfEmpty().
string[] oldPets2 =
    pets.AsQueryable()
    .Where(pet => pet.Age >= 10)
    .Select(pet => pet.Name)
    .ToArray();

// There may be no elements in the array, so directly
// accessing element 0 may throw an exception.
try
{
    Console.WriteLine("Second query: " + oldPets2[0]);
}
catch (Exception e)
{
    Console.WriteLine("Second query: An exception was thrown: " + e.Message);
}

/*
    This code produces the following output:

    First query: [EMPTY]
    Second query: An exception was thrown: Index was outside the bounds of the array.
*/

Remarks

The DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method generates a MethodCallExpression that represents calling DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) depends on the implementation of the type of the source parameter. The expected behavior is that it returns source if it is not empty. Otherwise, it returns an IQueryable<T> that contains defaultValue.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

DefaultIfEmpty<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.

C#
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource>(this System.Linq.IQueryable<TSource> source);
C#
public static System.Linq.IQueryable<TSource?> DefaultIfEmpty<TSource>(this System.Linq.IQueryable<TSource> source);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

The IQueryable<T> to return a default value for if empty.

Returns

IQueryable<TSource>

An IQueryable<T> that contains default(TSource) if source is empty; otherwise, source.

Exceptions

source is null.

Examples

The following code examples demonstrate how to use DefaultIfEmpty<TSource>(IQueryable<TSource>) to provide a default value in case the source sequence is empty.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void DefaultIfEmptyEx1()
{
    // Create a list of Pet objects.
    List<Pet> pets =
        new List<Pet>{ new Pet { Name="Barley", Age=8 },
                       new Pet { Name="Boots", Age=4 },
                       new Pet { Name="Whiskers", Age=1 } };

    // Call DefaultIfEmtpy() on the collection that Select()
    // returns, so that if the initial list is empty, there
    // will always be at least one item in the returned array.
    string[] names =
        pets.AsQueryable()
        .Select(pet => pet.Name)
        .DefaultIfEmpty()
        .ToArray();

    string first = names[0];
    Console.WriteLine(first);
}

/*
    This code produces the following output:

    Barley
*/

Remarks

The DefaultIfEmpty<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling DefaultIfEmpty<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling DefaultIfEmpty<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is that it returns source if it is not empty. Otherwise, it returns an IQueryable<T> that contains default(TSource).

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0