Aracılığıyla paylaş


Queryable.Count Yöntem

Tanım

Bir dizideki öğe sayısını döndürür.

Aşırı Yüklemeler

Name Description
Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Belirtilen dizideki bir koşulu karşılayan öğe sayısını döndürür.

Count<TSource>(IQueryable<TSource>)

Bir dizideki öğe sayısını döndürür.

Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs

Belirtilen dizideki bir koşulu karşılayan öğe sayısını döndürür.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static int Count(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static int Count<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 int Count<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Count : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> int
[<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 Count : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> int
<Extension()>
Public Function Count(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As Integer

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IQueryable<TSource>

IQueryable<T> Sayılacak öğeleri içeren.

predicate
Expression<Func<TSource,Boolean>>

Bir koşul için her öğeyi test etmek için bir işlev.

Döndürülenler

Koşul işlevindeki koşulu karşılayan dizideki öğelerin sayısı.

Öznitelikler

Özel durumlar

source veya predicate şeklindedir null.

içindeki source öğe sayısı Int32.MaxValue değerinden büyüktür.

Örnekler

Aşağıdaki kod örneği, bir koşulu karşılayan bir dizideki öğeleri saymak için nasıl kullanılacağını Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) gösterir.

class Pet
{
    public string Name { get; set; }
    public bool Vaccinated { get; set; }
}

public static void CountEx2()
{
    // Create an array of Pet objects.
    Pet[] pets = { new Pet { Name="Barley", Vaccinated=true },
                   new Pet { Name="Boots", Vaccinated=false },
                   new Pet { Name="Whiskers", Vaccinated=false } };

    // Count the number of unvaccinated pets in the array.
    int numberUnvaccinated =
        pets.AsQueryable().Count(p => !p.Vaccinated);

    Console.WriteLine(
        "There are {0} unvaccinated animals.",
        numberUnvaccinated);
}

// This code produces the following output:
//
// There are 2 unvaccinated animals.
Structure Pet
    Public Name As String
    Public Vaccinated As Boolean
End Structure

Shared Sub CountEx2()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Vaccinated = True}, _
                   New Pet With {.Name = "Boots", .Vaccinated = False}, _
                   New Pet With {.Name = "Whiskers", .Vaccinated = False}}

    ' Count the number of unvaccinated pets in the array.
    Dim numberUnvaccinated As Integer = pets.AsQueryable().Count(Function(p) p.Vaccinated = False)

    MsgBox(String.Format("There are {0} unvaccinated animals.", numberUnvaccinated))
End Sub

' This code produces the following output:
'
' There are 2 unvaccinated animals.

Açıklamalar

Bu yöntem, tür bağımsız değişkeni türlerden Expression<TDelegate> biri olan en az bir tür parametresine Func<T,TResult> sahiptir. Bu parametreler için bir lambda ifadesi geçirebilirsiniz ve bu ifade bir Expression<TDelegate>olarak derlenir.

yöntemi, Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) bir oluşturur. Ardından parametresinin özelliği tarafından Provider temsil edilen yöntemine sourceIQueryProvider iletirMethodCallExpression.Execute<TResult>(Expression)

Çağrıyı Count<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ış, tarafından predicatebelirtilen koşulu karşılayan öğe source sayısını sayıyor olmasıdır.

Şunlara uygulanır

Count<TSource>(IQueryable<TSource>)

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs

Bir dizideki öğe sayısını döndürür.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static int Count(System::Linq::IQueryable<TSource> ^ source);
public static int Count<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 int Count<TSource>(this System.Linq.IQueryable<TSource> source);
static member Count : System.Linq.IQueryable<'Source> -> int
[<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 Count : System.Linq.IQueryable<'Source> -> int
<Extension()>
Public Function Count(Of TSource) (source As IQueryable(Of TSource)) As Integer

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IQueryable<TSource>

IQueryable<T> Sayılacak öğeleri içeren.

Döndürülenler

Giriş dizisindeki öğelerin sayısı.

Öznitelikler

Özel durumlar

source, null'e eşittir.

içindeki source öğe sayısı Int32.MaxValue değerinden büyüktür.

Örnekler

Aşağıdaki kod örneği, bir dizideki öğeleri saymak için nasıl kullanılacağını Count<TSource>(IQueryable<TSource>) gösterir.

string[] fruits = { "apple", "banana", "mango",
                    "orange", "passionfruit", "grape" };

int numberOfFruits = fruits.AsQueryable().Count();

Console.WriteLine(
    "There are {0} items in the array.",
    numberOfFruits);

// This code produces the following output:
//
// There are 6 items in the array.
Dim fruits() As String = {"apple", "banana", "mango", _
                    "orange", "passionfruit", "grape"}

Dim numberOfFruits As Integer = fruits.AsQueryable().Count()

MsgBox(String.Format( _
    "There are {0} items in the array.", _
    numberOfFruits))

' This code produces the following output:
'
' There are 6 items in the array.

Açıklamalar

yöntemi, Count<TSource>(IQueryable<TSource>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran Count<TSource>(IQueryable<TSource>) bir oluşturur. Ardından parametresinin özelliği tarafından Provider temsil edilen yöntemine sourceIQueryProvider iletirMethodCallExpression.Execute<TResult>(Expression)

Çağrıyı Count<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 sourceöğe sayısını sayıyor olmasıdır.

Şunlara uygulanır