Queryable.DefaultIfEmpty 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回序列中的項目;如果序列是空的,則傳回預設值單一集合。
多載
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) |
傳回指定之序列的項目;如果序列是空的,則傳回單一集合中型別參數的預設值。 |
DefaultIfEmpty<TSource>(IQueryable<TSource>) |
傳回指定之序列的項目;如果序列是空的,則傳回單一集合中型別參數的預設值。 |
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回指定之序列的項目;如果序列是空的,則傳回單一集合中型別參數的預設值。
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
在序列空白時所要傳回的值。
傳回
如果 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) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery<TElement>(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource
。
執行表示呼叫 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source
。 預期的行為是,如果不是空的,則會傳 source
回它。 否則,它會傳 IQueryable<T> 回包含 的 defaultValue
。
適用於
DefaultIfEmpty<TSource>(IQueryable<TSource>)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- 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>。
傳回
如果 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>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery<TElement>(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource
。
執行表示呼叫 DefaultIfEmpty<TSource>(IQueryable<TSource>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source
。 預期的行為是,如果不是空的,則會傳 source
回它。 否則,它會傳 IQueryable<T> 回包含 的 default(TSource)
。