Bagikan melalui


Queryable.LongCount Metode

Definisi

Mengembalikan yang Int64 menunjukkan jumlah elemen secara berurutan.

Overload

Nama Deskripsi
LongCount<TSource>(IQueryable<TSource>)

Mengembalikan Int64 yang menunjukkan jumlah total elemen secara berurutan.

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

Mengembalikan yang Int64 menunjukkan jumlah elemen dalam urutan yang memenuhi kondisi.

LongCount<TSource>(IQueryable<TSource>)

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Mengembalikan Int64 yang menunjukkan jumlah total elemen secara berurutan.

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

Jenis parameter

TSource

Jenis elemen source.

Parameter

source
IQueryable<TSource>

Yang IQueryable<T> berisi elemen yang akan dihitung.

Mengembalikan

Jumlah elemen dalam source.

Atribut

Pengecualian

source adalah null.

Jumlah elemen melebihi Int64.MaxValue.

Contoh

Contoh kode berikut menunjukkan cara menggunakan LongCount<TSource>(IQueryable<TSource>) untuk menghitung elemen dalam array.

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

long count = fruits.AsQueryable().LongCount();

Console.WriteLine("There are {0} fruits in the collection.", count);

/*
    This code produces the following output:

    There are 6 fruits in the collection.
*/
Dim fruits() As String = {"apple", "banana", "mango", _
                      "orange", "passionfruit", "grape"}

Dim count As Long = fruits.AsQueryable().LongCount()

MsgBox(String.Format("There are {0} fruits in the collection.", count))

' This code produces the following output:

' There are 6 fruits in the collection.

Keterangan

Metode ini LongCount<TSource>(IQueryable<TSource>) menghasilkan MethodCallExpression yang mewakili pemanggilan LongCount<TSource>(IQueryable<TSource>) dirinya sebagai metode generik yang dibangun. Kemudian meneruskan MethodCallExpression ke Execute<TResult>(Expression) metode yang diwakili IQueryProvider oleh Provider properti source parameter.

Perilaku kueri yang terjadi sebagai akibat dari menjalankan pohon ekspresi yang mewakili panggilan LongCount<TSource>(IQueryable<TSource>) tergantung pada implementasi jenis source parameter. Perilaku yang diharapkan adalah menghitung jumlah item dalam source dan mengembalikan Int64.

Berlaku untuk

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

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Mengembalikan yang Int64 menunjukkan jumlah elemen dalam urutan yang memenuhi kondisi.

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

Jenis parameter

TSource

Jenis elemen source.

Parameter

source
IQueryable<TSource>

Yang IQueryable<T> berisi elemen yang akan dihitung.

predicate
Expression<Func<TSource,Boolean>>

Fungsi untuk menguji setiap elemen untuk kondisi.

Mengembalikan

Jumlah elemen dalam source yang memenuhi kondisi dalam fungsi predikat.

Atribut

Pengecualian

source atau predicate adalah null.

Jumlah elemen yang cocok melebihi Int64.MaxValue.

Contoh

Contoh kode berikut menunjukkan cara menggunakan LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) untuk menghitung elemen dalam array yang memenuhi kondisi.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void LongCountEx2()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    const int Age = 3;

    // Count the number of Pet objects where Pet.Age is greater than 3.
    long count = pets.AsQueryable().LongCount(pet => pet.Age > Age);

    Console.WriteLine("There are {0} animals over age {1}.", count, Age);
}

/*
    This code produces the following output:

    There are 2 animals over age 3.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Shared Sub LongCountEx2()
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
                       New Pet With {.Name = "Boots", .Age = 4}, _
                       New Pet With {.Name = "Whiskers", .Age = 1}}

    Const Age As Integer = 3

    ' Count the number of Pet objects where Pet.Age is greater than 3.
    Dim count As Long = pets.AsQueryable().LongCount(Function(Pet) Pet.Age > Age)

    MsgBox(String.Format("There are {0} animals over age {1}.", count, Age))
End Sub

' This code produces the following output:

' There are 2 animals over age 3.

Keterangan

Metode ini memiliki setidaknya satu parameter jenis Expression<TDelegate> yang argumen jenisnya adalah salah satu jenisnya Func<T,TResult> . Untuk parameter ini, Anda dapat meneruskan ekspresi lambda dan akan dikompilasi ke Expression<TDelegate>.

Metode ini LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) menghasilkan MethodCallExpression yang mewakili pemanggilan LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) dirinya sebagai metode generik yang dibangun. Kemudian meneruskan MethodCallExpression ke Execute<TResult>(Expression) metode yang diwakili IQueryProvider oleh Provider properti source parameter.

Perilaku kueri yang terjadi sebagai akibat dari menjalankan pohon ekspresi yang mewakili panggilan LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) tergantung pada implementasi jenis source parameter. Perilaku yang diharapkan adalah bahwa ia menghitung jumlah item dalam source yang memenuhi kondisi yang ditentukan oleh predicate dan mengembalikan Int64.

Berlaku untuk