Queryable.DefaultIfEmpty 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳序列中的元素,或若序列為空,則回傳預設值的單例集合。
多載
| 名稱 | Description |
|---|---|
| DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) |
如果序列是空的,則傳回指定序列的專案或單一集合中的指定值。 |
| DefaultIfEmpty<TSource>(IQueryable<TSource>) |
如果序列是空的,則傳回指定序列的專案或單一集合中型別參數的預設值。 |
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- 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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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<T>一個包含若defaultValue為source空則為空;否則,。 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由參數Provider性質source所表示的方法。
執行代表呼叫 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是如果它不是空的,它會回傳 source 。 否則,它會回傳包含 IQueryable<T>defaultValue的 。
適用於
DefaultIfEmpty<TSource>(IQueryable<TSource>)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- 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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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> 回傳預設值。
傳回
一個包含(TSourcedefault)的若IQueryable<T>為source空;否則,。 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由參數Provider性質source所表示的方法。
執行代表呼叫 DefaultIfEmpty<TSource>(IQueryable<TSource>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是如果它不是空的,它會回傳 source 。 否則,它會回傳包含 IQueryable<T>default(TSource)的 。