Queryable.Count Yöntem

Tanım

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

Aşırı Yüklemeler

Count<TSource>(IQueryable<TSource>)

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

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

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

Count<TSource>(IQueryable<TSource>)

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

Bir dizideki öğelerin 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);
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ı.

Özel durumlar

source, null değeridir.

içindeki source öğelerin sayısı Int32.MaxValue değerinden büyük.

Ö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 MethodCallExpressionExecute<TResult>(Expression) özelliği tarafından temsil edilen yöntemine IQueryProviderProvidersource iletir.

Ç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

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

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

Belirtilen dizideki bir koşulu karşılayan öğelerin 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);
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 bir.

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ı.

Özel durumlar

source veya predicate şeklindedir null.

içindeki source öğelerin sayısı Int32.MaxValue değerinden büyük.

Ö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 == false);

    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 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, 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 MethodCallExpressionExecute<TResult>(Expression) özelliği tarafından temsil edilen yöntemine IQueryProviderProvidersource iletir.

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

Şunlara uygulanır