Queryable.DefaultIfEmpty 方法

定义

返回序列中的元素,如果序列为空,则返回具有默认值的单一实例集合。

重载

DefaultIfEmpty<TSource>(IQueryable<TSource>)

返回指定序列中的元素;如果序列为空,则返回单一实例集合中的类型参数的默认值。

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

返回指定序列中的元素;如果序列为空,则返回单一实例集合中的指定值。

DefaultIfEmpty<TSource>(IQueryable<TSource>)

返回指定序列中的元素;如果序列为空,则返回单一实例集合中的类型参数的默认值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ DefaultIfEmpty(System::Linq::IQueryable<TSource> ^ source);
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource> (this System.Linq.IQueryable<TSource> source);
public static System.Linq.IQueryable<TSource?> DefaultIfEmpty<TSource> (this System.Linq.IQueryable<TSource> source);
static member DefaultIfEmpty : System.Linq.IQueryable<'Source> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function DefaultIfEmpty(Of TSource) (source As IQueryable(Of TSource)) As IQueryable(Of TSource)

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

用于在序列为空的情况下返回默认值的 IQueryable<T>

返回

IQueryable<TSource>

用于在 TSource 为空的情况下包含 default(source) 的 IQueryable<T>;否则为 source

例外

sourcenull

示例

下面的代码示例演示如何在源序列为空时使用 DefaultIfEmpty<TSource>(IQueryable<TSource>) 提供默认值。

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
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Shared Sub DefaultIfEmptyEx1()
    ' Create a list of Pet objects.
    Dim pets As New List(Of Pet)(New Pet() { _
                        New Pet With {.Name = "Barley", .Age = 8}, _
                        New Pet With {.Name = "Boots", .Age = 4}, _
                        New Pet With {.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.
    Dim names() As String = pets.AsQueryable() _
        .Select(Function(Pet) Pet.Name) _
        .DefaultIfEmpty() _
        .ToArray()

    Dim first As String = names(0)
    MsgBox(first)

    ' This code produces the following output:
    '
    ' Barley

End Sub

注解

方法 DefaultIfEmpty<TSource>(IQueryable<TSource>) 生成一个 , MethodCallExpression 表示将调用 DefaultIfEmpty<TSource>(IQueryable<TSource>) 自身作为构造的泛型方法。 然后,MethodCallExpressionCreateQuery<TElement>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 DefaultIfEmpty<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于 参数类型的 source 实现。 预期的行为是,如果它不为空, source 则返回 。 否则,它将返回 IQueryable<T> 包含 的 default(TSource)

适用于

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

返回指定序列中的元素;如果序列为空,则返回单一实例集合中的指定值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ DefaultIfEmpty(System::Linq::IQueryable<TSource> ^ source, TSource defaultValue);
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource> (this System.Linq.IQueryable<TSource> source, TSource defaultValue);
static member DefaultIfEmpty : System.Linq.IQueryable<'Source> * 'Source -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function DefaultIfEmpty(Of TSource) (source As IQueryable(Of TSource), defaultValue As TSource) As IQueryable(Of TSource)

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

用于在序列为空的情况下返回指定值的 IQueryable<T>

defaultValue
TSource

序列为空时要返回的值。

返回

IQueryable<TSource>

如果 source 为空,则为包含 defaultValueIQueryable<T>;否则为 source

例外

sourcenull

示例

下面的代码示例演示在 LINQ 查询中调用 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 非常有用的情况。 在此示例中, DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 将默认值传递给 。

// 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.
*/
' Create a list of Pet objects.
Dim pets As New List(Of Pet)(New Pet() { _
                   New Pet With {.Name = "Barley", .Age = 8}, _
                   New Pet With {.Name = "Boots", .Age = 4}, _
                   New Pet With {.Name = "Whiskers", .Age = 1}})

' This query returns 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().
Dim oldPets() As String = pets.AsQueryable() _
    .Where(Function(Pet) Pet.Age >= 10) _
    .Select(Function(Pet) Pet.Name) _
    .DefaultIfEmpty("[EMPTY]") _
    .ToArray()
Try
    MsgBox("First query: " + oldPets(0))
Catch ex As Exception
    Console.WriteLine("First query: An exception was thrown: " + ex.Message)
End Try

' This query selects only those pets that are 10 or older.
' This code does not call DefaultIfEmpty().
Dim oldPets2() As String = _
    pets.AsQueryable() _
    .Where(Function(Pet) Pet.Age >= 10) _
    .Select(Function(Pet) Pet.Name) _
    .ToArray()

' There may be no elements in the array, so directly
' accessing element 0 may throw an exception.
Try
    MsgBox("Second query: " + oldPets2(0))
Catch ex As Exception
    MsgBox("Second query: An exception was thrown: " + ex.Message)
End Try

' This code produces the following output:
'
' First(query) : [EMPTY]
' Second query: An exception was thrown: Index was outside the bounds of the array.

注解

方法 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 生成一个 , MethodCallExpression 表示将调用 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 自身作为构造的泛型方法。 然后,MethodCallExpressionCreateQuery<TElement>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 的表达式树而发生的查询行为取决于 参数类型的 source 实现。 预期的行为是,如果它不为空, source 则返回 。 否则,它将返回 IQueryable<T> 包含 的 defaultValue

适用于