Queryable.FirstOrDefault 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳序列的第一個元素,若未找到則返回預設值。
多載
| 名稱 | Description |
|---|---|
| FirstOrDefault<TSource>(IQueryable<TSource>, TSource) |
傳回序列的第一個專案,如果序列不包含任何專案,則傳回預設值。 |
| FirstOrDefault<TSource>(IQueryable<TSource>) |
傳回序列的第一個專案,如果序列不包含任何專案,則傳回預設值。 |
| FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) |
傳回序列的第一個專案,如果找不到指定的條件,則傳回預設值。 |
| FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, TSource) |
傳回序列的第一個專案,如果找不到這類專案,則為符合條件或預設值。 |
FirstOrDefault<TSource>(IQueryable<TSource>, TSource)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回序列的第一個專案,如果序列不包含任何專案,則傳回預設值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource FirstOrDefault(System::Linq::IQueryable<TSource> ^ source, TSource defaultValue);
public static TSource FirstOrDefault<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 TSource FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, TSource defaultValue);
static member FirstOrDefault : System.Linq.IQueryable<'Source> * 'Source -> '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 FirstOrDefault : System.Linq.IQueryable<'Source> * 'Source -> 'Source
<Extension()>
Public Function FirstOrDefault(Of TSource) (source As IQueryable(Of TSource), defaultValue As TSource) As TSource
類型參數
- TSource
元素 source的類型。
參數
- source
- IQueryable<TSource>
IEnumerable<T>還原第一個元素。
- defaultValue
- TSource
如果序列為空,則會回傳預設值。
傳回
defaultValue 若 source 為空;否則,為 中的 source第一個元素。
- 屬性
例外狀況
source 是 null。
適用於
FirstOrDefault<TSource>(IQueryable<TSource>)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回序列的第一個專案,如果序列不包含任何專案,則傳回預設值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource FirstOrDefault(System::Linq::IQueryable<TSource> ^ source);
public static TSource FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source);
public static TSource? FirstOrDefault<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 TSource? FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source);
static member FirstOrDefault : System.Linq.IQueryable<'Source> -> '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 FirstOrDefault : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function FirstOrDefault(Of TSource) (source As IQueryable(Of TSource)) As TSource
類型參數
- TSource
元素 source的類型。
參數
- source
- IQueryable<TSource>
IQueryable<T>還原第一個元素。
傳回
default(TSource) 若為 source 空;否則,為 中的 source第一個元素。
- 屬性
例外狀況
source 是 null。
範例
以下程式碼範例示範如何在空序列上使用 FirstOrDefault<TSource>(IQueryable<TSource>) 。
// Create an empty array.
int[] numbers = { };
// Get the first item in the array, or else the
// default value for type int (0).
int first = numbers.AsQueryable().FirstOrDefault();
Console.WriteLine(first);
/*
This code produces the following output:
0
*/
' Create an empty array.
Dim numbers() As Integer = {}
' Get the first item in the array, or else the
' default value for type int, which is 0.
Dim first As Integer = numbers.AsQueryable().FirstOrDefault()
MsgBox(first)
' This code produces the following output:
' 0
有時候如果集合中沒有任何元素,該 default(TSource) 值並非你想使用的預設值。 你不必檢查結果是否有不需要的預設值,必要時再更改,而是可以用這個 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 方法指定如果集合是空的,你想用的預設值。 接著,呼叫 First<TSource>(IQueryable<TSource>) 以獲得第一個元素。 以下程式碼範例結合這兩種技術,若一組數值月份為空,則預設值為 1。 由於整數的預設值為 0,且不對應任何月份,因此預設值必須指定為 1。 查詢完成後,會檢查第一個結果變數是否為不想要的預設值。 第二個結果變數是透過呼叫 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 指定預設值 1 來取得的。
List<int> months = new List<int> { };
// Setting the default value to 1 after the query.
int firstMonth1 = months.AsQueryable().FirstOrDefault();
if (firstMonth1 == 0)
{
firstMonth1 = 1;
}
Console.WriteLine("The value of the firstMonth1 variable is {0}", firstMonth1);
// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int firstMonth2 = months.AsQueryable().DefaultIfEmpty(1).First();
Console.WriteLine("The value of the firstMonth2 variable is {0}", firstMonth2);
/*
This code produces the following output:
The value of the firstMonth1 variable is 1
The value of the firstMonth2 variable is 1
*/
Dim months As New List(Of Integer)(New Integer() {})
' Setting the default value to 1 after the query.
Dim firstMonth1 As Integer = months.AsQueryable().FirstOrDefault()
If firstMonth1 = 0 Then
firstMonth1 = 1
End If
MsgBox(String.Format("The value of the firstMonth1 variable is {0}", firstMonth1))
' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim firstMonth2 As Integer = months.AsQueryable().DefaultIfEmpty(1).First()
MsgBox(String.Format("The value of the firstMonth2 variable is {0}", firstMonth2))
' This code produces the following output:
'
' The value of the firstMonth1 variable is 1
' The value of the firstMonth2 variable is 1
備註
該 FirstOrDefault<TSource>(IQueryable<TSource>) 方法產生的 是 MethodCallExpression ,代表 FirstOrDefault<TSource>(IQueryable<TSource>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpression給Execute<TResult>(Expression)IQueryProvider由參數Provider性質source所表示的方法。
執行代表呼叫 FirstOrDefault<TSource>(IQueryable<TSource>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期的行為是回傳 中 source第一個元素,若 source 為空則返回預設值。
此 FirstOrDefault 方法未提供若為空,則指定要回傳 source 的預設值。 如果你想指定非 default(TSource)的預設值,請使用 DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) 範例章節中所述的方法。
適用於
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回序列的第一個專案,如果找不到指定的條件,則傳回預設值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource FirstOrDefault(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
public static TSource? FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
[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 TSource? FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member FirstOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> '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 FirstOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function FirstOrDefault(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource
類型參數
- TSource
元素 source的類型。
參數
- source
- IQueryable<TSource>
一個 IQueryable<T> 還原元素的來源。
- predicate
- Expression<Func<TSource,Boolean>>
一個用來測試每個元素條件的函數。
傳回
default(TSource) 若為source空或無元素通過由 predicate所指定的測試;否則,中第一個通過由 source所指定的測試的元素predicate。
- 屬性
例外狀況
source 或 predicate 為 null。
範例
以下程式碼範例示範如何透過傳遞謂詞來使用 FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 。 在第二個查詢中,序列中沒有任何元素滿足該條件。
string[] names = { "Hartono, Tommy", "Adams, Terry",
"Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
// Get the first string in the array that is longer
// than 20 characters, or the default value for type
// string (null) if none exists.
string firstLongName =
names.AsQueryable().FirstOrDefault(name => name.Length > 20);
Console.WriteLine("The first long name is '{0}'.", firstLongName);
// Get the first string in the array that is longer
// than 30 characters, or the default value for type
// string (null) if none exists.
string firstVeryLongName =
names.AsQueryable().FirstOrDefault(name => name.Length > 30);
Console.WriteLine(
"There is {0} name that is longer than 30 characters.",
string.IsNullOrEmpty(firstVeryLongName) ? "NOT a" : "a");
/*
This code produces the following output:
The first long name is 'Andersen, Henriette Thaulow'.
There is NOT a name that is longer than 30 characters.
*/
Dim names() As String = {"Hartono, Tommy", "Adams, Terry", _
"Andersen, Henriette Thaulow", _
"Hedlund, Magnus", "Ito, Shu"}
' Get the first string in the array that is longer
' than 20 characters, or the default value for type
' string (null) if none exists.
Dim firstLongName As String = _
names.AsQueryable().FirstOrDefault(Function(name) name.Length > 20)
MsgBox(String.Format("The first long name is '{0}'.", firstLongName))
' Get the first string in the array that is longer
' than 30 characters, or the default value for type
' string (null) if none exists.
Dim firstVeryLongName As String = _
names.AsQueryable().FirstOrDefault(Function(name) name.Length > 30)
MsgBox(String.Format( _
"There is {0} name that is longer than 30 characters.", _
IIf(String.IsNullOrEmpty(firstVeryLongName), "NOT a", "a")))
' This code produces the following output:
'
' The first long name is 'Andersen, Henriette Thaulow'.
' There is NOT a name that is longer than 30 characters.
備註
此方法至少有一個型別 Expression<TDelegate> 參數,其型別參數的參數是其中一個 Func<T,TResult> 型別。 對於這些參數,你可以輸入 lambda 運算式,然後它會被編譯成 Expression<TDelegate>。
該 FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 方法產生的 是 MethodCallExpression ,代表 FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpression給Execute<TResult>(Expression)IQueryProvider由參數Provider性質source所表示的方法。
執行代表呼叫 FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是回傳第一個符合條件的source元素predicate;若無元素滿足條件,則回傳預設值。
適用於
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, TSource)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回序列的第一個專案,如果找不到這類專案,則為符合條件或預設值。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource FirstOrDefault(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate, TSource defaultValue);
public static TSource FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate, 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 TSource FirstOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate, TSource defaultValue);
static member FirstOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> * 'Source -> '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 FirstOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> * 'Source -> 'Source
<Extension()>
Public Function FirstOrDefault(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean)), defaultValue As TSource) As TSource
類型參數
- TSource
元素 source的類型。
參數
- source
- IQueryable<TSource>
一個 IEnumerable<T> 還原元素的來源。
- predicate
- Expression<Func<TSource,Boolean>>
一個用來測試每個元素條件的函數。
- defaultValue
- TSource
如果序列為空,則會回傳預設值。
傳回
defaultValue若source為空,或無元素通過由 predicate指定的測試;否則,第一個通過由 predicate所指定的測試的元素source。
- 屬性
例外狀況
source 或 predicate 為 null。