Share via


DbQuery<TQuery> Class

Definition

Caution

Use DbSet<T> instead

A DbQuery<TQuery> can be used to query instances of TQuery. LINQ queries against a DbQuery<TQuery> will be translated into queries against the database.

The results of a LINQ query against a DbQuery<TQuery> will contain the results returned from the database and may not reflect changes made in the context that have not been persisted to the database. For example, the results will not contain newly added views and may still contain views that are marked for deletion.

Depending on the database being used, some parts of a LINQ query against a DbQuery<TQuery> may be evaluated in memory rather than being translated into a database query.

DbQuery<TQuery> objects are usually obtained from a DbQuery<TQuery> property on a derived DbContext or from the Query<TQuery>() method.

public abstract class DbQuery<TQuery> : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TQuery>, System.Collections.Generic.IEnumerable<TQuery>, System.Linq.IQueryable<TQuery> where TQuery : class
[System.Obsolete("Use DbSet<T> instead")]
public abstract class DbQuery<TQuery> : Microsoft.EntityFrameworkCore.DbSet<TQuery> where TQuery : class
type DbQuery<'Query (requires 'Query : null)> = class
    interface IQueryable<'Query (requires 'Query : null)>
    interface seq<'Query (requires 'Query : null)>
    interface IEnumerable
    interface IQueryable
    interface IAsyncEnumerableAccessor<'Query (requires 'Query : null)>
    interface IInfrastructure<IServiceProvider>
[<System.Obsolete("Use DbSet<T> instead")>]
type DbQuery<'Query (requires 'Query : null)> = class
    inherit DbSet<'Query (requires 'Query : null)>
Public MustInherit Class DbQuery(Of TQuery)
Implements IAsyncEnumerableAccessor(Of TQuery), IEnumerable(Of TQuery), IInfrastructure(Of IServiceProvider), IQueryable(Of TQuery)
Public MustInherit Class DbQuery(Of TQuery)
Inherits DbSet(Of TQuery)

Type Parameters

TQuery

The type of view being operated on by this view.

Inheritance
DbQuery<TQuery>
Inheritance
DbSet<TQuery>
DbQuery<TQuery>
Attributes
Implements
IInfrastructure<IServiceProvider> Microsoft.EntityFrameworkCore.Query.Internal.IAsyncEnumerableAccessor<TQuery> IEnumerable<T> IEnumerable<TQuery> IEnumerable IQueryable IQueryable<TQuery>

Constructors

DbQuery<TQuery>()

Properties

EntityType

The IEntityType metadata associated with this set.

(Inherited from DbSet<TEntity>)
Local

Gets a LocalView<TEntity> that represents a local view of all Added, Unchanged, and Modified entities in this set.

(Inherited from DbSet<TEntity>)

Methods

Add(TEntity)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
AddAsync(TEntity, CancellationToken)

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
AddRange(IEnumerable<TEntity>)

Begins tracking the given entities, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
AddRange(TEntity[])

Begins tracking the given entities, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
AddRangeAsync(IEnumerable<TEntity>, CancellationToken)

Begins tracking the given entities, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
AddRangeAsync(TEntity[])

Begins tracking the given entities, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
AsAsyncEnumerable()

Returns this object typed as IAsyncEnumerable<T>.

(Inherited from DbSet<TEntity>)
AsQueryable()

Returns this object typed as IQueryable<T>.

(Inherited from DbSet<TEntity>)
Attach(TEntity)

Begins tracking the given entity and entries reachable from the given entity using the Unchanged state by default, but see below for cases when a different state will be used.

(Inherited from DbSet<TEntity>)
AttachRange(IEnumerable<TEntity>)

Begins tracking the given entities and entries reachable from the given entities using the Unchanged state by default, but see below for cases when a different state will be used.

(Inherited from DbSet<TEntity>)
AttachRange(TEntity[])

Begins tracking the given entities and entries reachable from the given entities using the Unchanged state by default, but see below for cases when a different state will be used.

(Inherited from DbSet<TEntity>)
Entry(TEntity)

Gets an EntityEntry<TEntity> for the given entity. The entry provides access to change tracking information and operations for the entity.

(Inherited from DbSet<TEntity>)
Find(Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

(Inherited from DbSet<TEntity>)
FindAsync(Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

(Inherited from DbSet<TEntity>)
FindAsync(Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.

(Inherited from DbSet<TEntity>)
GetAsyncEnumerator(CancellationToken)

Returns an IAsyncEnumerator<T> which when enumerated will asynchronously execute a query against the database.

(Inherited from DbSet<TEntity>)
Remove(TEntity)

Begins tracking the given entity in the Deleted state such that it will be removed from the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
RemoveRange(IEnumerable<TEntity>)

Begins tracking the given entities in the Deleted state such that they will be removed from the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
RemoveRange(TEntity[])

Begins tracking the given entities in the Deleted state such that they will be removed from the database when SaveChanges() is called.

(Inherited from DbSet<TEntity>)
Update(TEntity)

Begins tracking the given entity and entries reachable from the given entity using the Modified state by default, but see below for cases when a different state will be used.

(Inherited from DbSet<TEntity>)
UpdateRange(IEnumerable<TEntity>)

Begins tracking the given entities and entries reachable from the given entities using the Modified state by default, but see below for cases when a different state will be used.

(Inherited from DbSet<TEntity>)
UpdateRange(TEntity[])

Begins tracking the given entities and entries reachable from the given entities using the Modified state by default, but see below for cases when a different state will be used.

(Inherited from DbSet<TEntity>)

Explicit Interface Implementations

IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken)

Returns an IAsyncEnumerator<T> which when enumerated will asynchronously execute a query against the database.

(Inherited from DbSet<TEntity>)
IAsyncEnumerableAccessor<TQuery>.AsyncEnumerable

Returns an IAsyncEnumerable<T> which when enumerated will asynchronously execute the query against the database.

IEnumerable.GetEnumerator()

Returns an IEnumerator which when enumerated will execute a query against the database to load all views from the database.

IEnumerable.GetEnumerator()

Returns an IEnumerator which when enumerated will execute a query against the database to load all entities from the database.

(Inherited from DbSet<TEntity>)
IEnumerable<TEntity>.GetEnumerator()

Returns an IEnumerator<T> which when enumerated will execute a query against the database to load all entities from the database.

(Inherited from DbSet<TEntity>)
IEnumerable<TQuery>.GetEnumerator()

Returns an IEnumerator<T> which when enumerated will execute a query against the database to load all views from the database.

IInfrastructure<IServiceProvider>.Instance

Gets the scoped IServiceProvider being used to resolve services.

This property is intended for use by extension methods that need to make use of services not directly exposed in the public API surface.

IInfrastructure<IServiceProvider>.Instance

Gets the scoped IServiceProvider being used to resolve services.

(Inherited from DbSet<TEntity>)
IListSource.ContainsListCollection

Gets a value indicating whether the collection is a collection of System.Collections.IList objects. Always returns false.

(Inherited from DbSet<TEntity>)
IListSource.GetList()

This method is called by data binding frameworks when attempting to data bind directly to a DbSet<TEntity>.

(Inherited from DbSet<TEntity>)
IQueryable.ElementType

Gets the IQueryable element type.

IQueryable.ElementType

Gets the IQueryable element type.

(Inherited from DbSet<TEntity>)
IQueryable.Expression

Gets the IQueryable LINQ Expression.

IQueryable.Expression

Gets the IQueryable LINQ Expression.

(Inherited from DbSet<TEntity>)
IQueryable.Provider

Gets the IQueryable provider.

IQueryable.Provider

Gets the IQueryable provider.

(Inherited from DbSet<TEntity>)

Extension Methods

AllAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously determines whether all the elements of a sequence satisfy a condition.

AnyAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously determines whether any element of a sequence satisfies a condition.

AnyAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously determines whether a sequence contains any elements.

AsAsyncEnumerable<TSource>(IQueryable<TSource>)

Returns an IAsyncEnumerable<T> which can be enumerated asynchronously.

AsNoTracking<TEntity>(IQueryable<TEntity>)

The change tracker will not track any of the entities that are returned from a LINQ query. If the entity instances are modified, this will not be detected by the change tracker and SaveChanges() will not persist those changes to the database.

AsTracking<TEntity>(IQueryable<TEntity>, QueryTrackingBehavior)

Returns a new query where the change tracker will either keep track of changes or not for all entities that are returned, depending on the value of the 'track' parameter. When tracking, Any modification to the entity instances will be detected and persisted to the database during SaveChanges(). When not tracking, if the entity instances are modified, this will not be detected by the change tracker and SaveChanges() will not persist those changes to the database.

AsTracking<TEntity>(IQueryable<TEntity>)

Returns a new query where the change tracker will keep track of changes for all entities that are returned. Any modification to the entity instances will be detected and persisted to the database during SaveChanges().

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>, CancellationToken)

Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.

ContainsAsync<TSource>(IQueryable<TSource>, TSource, CancellationToken)

Asynchronously determines whether a sequence contains a specified element by using the default equality comparer.

CountAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the number of elements in a sequence that satisfy a condition.

CountAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the number of elements in a sequence.

FirstAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the first element of a sequence that satisfies a specified condition.

FirstAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the first element of a sequence.

FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.

FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.

ForEachAsync<T>(IQueryable<T>, Action<T>, CancellationToken)

Asynchronously enumerates the query results and performs the specified action on each element.

IgnoreQueryFilters<TEntity>(IQueryable<TEntity>)

Specifies that the current Entity Framework LINQ query should not have any model-level entity query filters applied.

Include<TEntity>(IQueryable<TEntity>, String)

Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried (TEntity). Further navigation properties to be included can be appended, separated by the '.' character.

Include<TEntity,TProperty>(IQueryable<TEntity>, Expression<Func<TEntity,TProperty>>)

Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried (TEntity). If you wish to include additional types based on the navigation properties of the type being included, then chain a call to ThenInclude<TEntity,TPreviousProperty,TProperty>(IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty, TProperty>>) after this call.

LastAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the last element of a sequence that satisfies a specified condition.

LastAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the last element of a sequence.

LastOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the last element of a sequence that satisfies a specified condition or a default value if no such element is found.

LastOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the last element of a sequence, or a default value if the sequence contains no elements.

Load<TSource>(IQueryable<TSource>)

Enumerates the query. When using Entity Framework, this causes the results of the query to be loaded into the associated context. This is equivalent to calling ToList and then throwing away the list (without the overhead of actually creating the list).

LoadAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously enumerates the query. When using Entity Framework, this causes the results of the query to be loaded into the associated context. This is equivalent to calling ToList and then throwing away the list (without the overhead of actually creating the list).

LongCountAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns a Int64 that represents the number of elements in a sequence that satisfy a condition.

LongCountAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns a Int64 that represents the total number of elements in a sequence.

MaxAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the maximum value of a sequence.

MaxAsync<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>, CancellationToken)

Asynchronously invokes a projection function on each element of a sequence and returns the maximum resulting value.

MinAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the minimum value of a sequence.

MinAsync<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>, CancellationToken)

Asynchronously invokes a projection function on each element of a sequence and returns the minimum resulting value.

SingleAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

SingleAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, CancellationToken)

Asynchronously returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

SingleOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>, CancellationToken)

Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.

TagWith<T>(IQueryable<T>, String)

Adds a tag to the collection of tags associated with an EF LINQ query. Tags are query annotations that can provide contextual tracing information at different points in the query pipeline.

ToArrayAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously creates an array from an IQueryable<T> by enumerating it asynchronously.

ToDictionaryAsync<TSource,TKey>(IQueryable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>, CancellationToken)

Creates a Dictionary<TKey,TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector function and a comparer.

ToDictionaryAsync<TSource,TKey>(IQueryable<TSource>, Func<TSource,TKey>, CancellationToken)

Creates a Dictionary<TKey,TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector function.

ToDictionaryAsync<TSource,TKey,TElement>(IQueryable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>, CancellationToken)

Creates a Dictionary<TKey,TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector function, a comparer, and an element selector function.

ToDictionaryAsync<TSource,TKey,TElement>(IQueryable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, CancellationToken)

Creates a Dictionary<TKey,TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector and an element selector function.

ToListAsync<TSource>(IQueryable<TSource>, CancellationToken)

Asynchronously creates a List<T> from an IQueryable<T> by enumerating it asynchronously.

GetInfrastructure<T>(IInfrastructure<T>)

Gets the value from a property that is being hidden using IInfrastructure<T>.

This method is typically used by database providers (and other extensions). It is generally not used in application code.

CreateProxy<TEntity>(DbSet<TEntity>, Object[])

Creates a proxy instance for an entity type if proxy creation has been turned on.

FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, Object[])
Obsolete.

Creates a LINQ query based on a raw SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators - context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name).

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter - context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm).

This overload also accepts DbParameter instances as parameter values. This allows you to use named parameters in the SQL query string - context.Blogs.FromSql("SELECT * FROM [dbo].[SearchBlogs]({@searchTerm})", new SqlParameter("@searchTerm", userSuppliedSearchTerm))

FromSql<TEntity>(IQueryable<TEntity>, FormattableString)
Obsolete.

Creates a LINQ query based on an interpolated string representing a SQL query.

If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators - context.Blogs.FromSql("SELECT * FROM dbo.Blogs").OrderBy(b => b.Name).

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include interpolated parameter place holders in the SQL query string. Any interpolated parameter values you supply will automatically be converted to a DbParameter - context.Blogs.FromSql($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})").

FromSqlInterpolated<TEntity>(DbSet<TEntity>, FormattableString)

Creates a LINQ query based on an interpolated string representing a SQL query.

FromSqlRaw<TEntity>(DbSet<TEntity>, String, Object[])

Creates a LINQ query based on a raw SQL query.

Applies to