Queryable.FirstOrDefault Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir dizinin ilk öğesini veya öğe bulunamazsa varsayılan değeri döndürür.
Aşırı Yüklemeler
| FirstOrDefault<TSource>(IQueryable<TSource>) |
Bir dizinin ilk öğesini veya dizi öğe içermiyorsa varsayılan değeri döndürür. |
| FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) |
Belirtilen koşulu karşılayan bir dizinin ilk öğesini veya böyle bir öğe bulunamazsa varsayılan değeri döndürür. |
| FirstOrDefault<TSource>(IQueryable<TSource>, TSource) |
Bir dizinin ilk öğesini veya dizi öğe içermiyorsa varsayılan değeri döndürür. |
| FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, TSource) |
Böyle bir öğe bulunamazsa, bir koşulu veya varsayılan değeri karşılayan dizinin ilk öğesini döndürür. |
FirstOrDefault<TSource>(IQueryable<TSource>)
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
Bir dizinin ilk öğesini veya dizi öğe içermiyorsa varsayılan değeri döndürür.
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);
static member FirstOrDefault : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function FirstOrDefault(Of TSource) (source As IQueryable(Of TSource)) As TSource
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IQueryable<TSource>
öğesinin IQueryable<T> ilk öğesini döndürmek için.
Döndürülenler
default(TSource) boşsa source ; yoksa içindeki sourceilk öğedir.
Özel durumlar
source, null değeridir.
Örnekler
Aşağıdaki kod örneği, boş bir dizide nasıl kullanılacağını FirstOrDefault<TSource>(IQueryable<TSource>) gösterir.
// 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) Bazen değeri, koleksiyon öğe içermiyorsa kullanmak istediğiniz varsayılan değer değildir. İstenmeyen varsayılan değerin sonucunu denetlemek ve gerekirse değiştirmek yerine yöntemini kullanarak DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) koleksiyon boşsa kullanmak istediğiniz varsayılan değeri belirtebilirsiniz. Ardından, ilk öğeyi almak için çağrısı First<TSource>(IQueryable<TSource>) yapın. Aşağıdaki kod örneği, sayısal aylardan oluşan bir koleksiyon boşsa 1 varsayılan değerini elde etmek için her iki tekniği de kullanır. Bir tamsayı için varsayılan değer herhangi bir aya karşılık gelen 0 olduğundan, bunun yerine varsayılan değer 1 olarak belirtilmelidir. sorgu tamamlandıktan sonra ilk sonuç değişkeni istenmeyen varsayılan değer için denetlendi. İkinci sonuç değişkeni, varsayılan değer olan 1'i belirtmek için çağrılarak DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) elde edilir.
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
Açıklamalar
yöntemi, FirstOrDefault<TSource>(IQueryable<TSource>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağırmayı FirstOrDefault<TSource>(IQueryable<TSource>) temsil eden bir oluşturur. Daha sonra parametresinin özelliği tarafından Provider temsil edilen yöntemine IQueryProvidersource iletirMethodCallExpression.Execute<TResult>(Expression)
Çağrıyı FirstOrDefault<TSource>(IQueryable<TSource>) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, içindeki sourceilk öğeyi veya boşsa source varsayılan değeri döndürmesidir.
FirstOrDefault yöntemi, boşsa source döndürülecek varsayılan değeri belirtmek için bir yol sağlamaz. dışında default(TSource)bir varsayılan değer belirtmek istiyorsanız, Örnek bölümünde açıklandığı gibi yöntemini kullanın DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) .
Şunlara uygulanır
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
Belirtilen koşulu karşılayan bir dizinin ilk öğesini veya böyle bir öğe bulunamazsa varsayılan değeri döndürür.
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);
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
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IQueryable<TSource>
Öğesi IQueryable<T> döndürülecek öğesi.
- predicate
- Expression<Func<TSource,Boolean>>
Bir koşul için her öğeyi test etmek için bir işlev.
Döndürülenler
default(TSource) boşsa source veya hiçbir öğe tarafından predicatebelirtilen testi geçmezse; aksi takdirde, içindeki source ilk öğe tarafından predicatebelirtilen testi geçirir.
Özel durumlar
source veya predicate şeklindedir null.
Örnekler
Aşağıdaki kod örneği, bir koşul geçirerek nasıl kullanılacağını FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) gösterir. İkinci sorguda, dizide koşulu karşılayan bir öğe yoktur.
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.
Açıklamalar
Bu yöntem, tür bağımsız değişkeni türlerden Expression<TDelegate> biri olan türünde en az bir parametreye Func<T,TResult> sahiptir. Bu parametreler için bir lambda ifadesi geçirebilirsiniz ve bu ifade bir Expression<TDelegate>olarak derlenir.
yöntemi, FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağırmayı FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) temsil eden bir oluşturur. Daha sonra parametresinin özelliği tarafından Provider temsil edilen yöntemine IQueryProvidersource iletirMethodCallExpression.Execute<TResult>(Expression)
Çağrıyı FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, içindeki source koşulu predicatekarşılayan ilk öğeyi veya koşulu karşılayan öğe yoksa varsayılan değeri döndürmesidir.
Şunlara uygulanır
FirstOrDefault<TSource>(IQueryable<TSource>, TSource)
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
Bir dizinin ilk öğesini veya dizi öğe içermiyorsa varsayılan değeri döndürür.
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);
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
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IQueryable<TSource>
öğesinin IEnumerable<T> ilk öğesini döndürmek için.
- defaultValue
- TSource
Dizi boşsa döndürülecek varsayılan değerdir.
Döndürülenler
defaultValue boşsa source ; değilse içindeki ilk öğedir source.
Özel durumlar
source, null değeridir.
Şunlara uygulanır
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, TSource)
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
- Kaynak:
- Queryable.cs
Böyle bir öğe bulunamazsa, bir koşulu veya varsayılan değeri karşılayan dizinin ilk öğesini döndürür.
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);
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
Tür Parametreleri
- TSource
öğelerinin sourcetürü.
Parametreler
- source
- IQueryable<TSource>
Öğesi IEnumerable<T> döndürülecek öğesi.
- predicate
- Expression<Func<TSource,Boolean>>
Bir koşul için her öğeyi test etmek için bir işlev.
- defaultValue
- TSource
Dizi boşsa döndürülecek varsayılan değerdir.
Döndürülenler
defaultValue boşsa source veya hiçbir öğe tarafından predicatebelirtilen testi geçmezse; aksi takdirde, içindeki source ilk öğe tarafından predicatebelirtilen testi geçirir.
Özel durumlar
source veya predicate şeklindedir null.