Queryable.LongCount(Of TSource) Method (IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean)))


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns an Int64 that represents the number of elements in a sequence that satisfy a condition.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)

Public Shared Function LongCount(Of TSource) (
	source As IQueryable(Of TSource),
	predicate As Expression(Of Func(Of TSource, Boolean))
) As Long


Type: System.Linq.IQueryable(Of TSource)

An IQueryable(Of T) that contains the elements to be counted.

Type: System.Linq.Expressions.Expression(Of Func(Of TSource, Boolean))

A function to test each element for a condition.

Return Value

Type: System.Int64

The number of elements in source that satisfy the condition in the predicate function.

Type Parameters


The type of the elements of source.

Exception Condition

source or predicate is null.


The number of matching elements exceeds MaxValue.

This method has at least one parameter of type Expression(Of TDelegate) whose type argument is one of the Func(Of T, TResult) types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression(Of TDelegate).

The LongCount(Of TSource)(IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean))) method generates a MethodCallExpression that represents calling LongCount(Of TSource)(IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean))) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute(Of TResult)(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling LongCount(Of TSource)(IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean))) depends on the implementation of the type of the source parameter. The expected behavior is that it counts the number of items in source that satisfy the condition specified by predicate and returns an Int64.

The following code example demonstrates how to use LongCount(Of TSource)(IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean))) to count the elements in an array that satisfy a condition.

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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top