Queryable.DefaultIfEmpty 메서드

정의

시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 기본값이 할당된 singleton 컬렉션을 반환합니다.

오버로드

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

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 singleton 컬렉션의 지정된 값을 반환합니다.

DefaultIfEmpty<TSource>(IQueryable<TSource>)

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 형식 매개 변수의 기본값을 반환합니다.

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

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

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 singleton 컬렉션의 지정된 값을 반환합니다.

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>

defaultValue가 비어 있으면 source가 들어 있는 IQueryable<T>이고, 그렇지 않으면 source입니다.

예외

source이(가) null인 경우

예제

다음 코드 예제에서는 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) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 CreateQuery<TElement>(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상되는 동작은 비어 있지 않으면 를 반환 source 하는 것입니다. 그렇지 않으면 가 포함된 를 IQueryable<T> 반환합니다 defaultValue.

적용 대상

DefaultIfEmpty<TSource>(IQueryable<TSource>)

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

지정된 시퀀스의 요소를 반환하거나, 시퀀스가 비어 있으면 형식 매개 변수의 기본값을 반환합니다.

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입니다.

예외

source이(가) null인 경우

예제

다음 코드 예제에서는 소스 시퀀스가 비어 있는 경우 를 사용하여 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>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 CreateQuery<TElement>(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 DefaultIfEmpty<TSource>(IQueryable<TSource>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상되는 동작은 비어 있지 않으면 를 반환 source 하는 것입니다. 그렇지 않으면 가 포함된 를 IQueryable<T> 반환합니다 default(TSource).

적용 대상