ObjectQuery<T> ObjectQuery<T> ObjectQuery<T> ObjectQuery<T> Class

Definition

Represents a typed query against a conceptual model in a given object context.

generic <typename T>
public ref class ObjectQuery : System::Data::Objects::ObjectQuery, System::Collections::Generic::IEnumerable<T>, System::ComponentModel::IListSource, System::Linq::IOrderedQueryable<T>, System::Linq::IQueryable<T>
public class ObjectQuery<T> : System.Data.Objects.ObjectQuery, System.Collections.Generic.IEnumerable<T>, System.ComponentModel.IListSource, System.Linq.IOrderedQueryable<T>, System.Linq.IQueryable<T>
type ObjectQuery<'T> = class
    inherit ObjectQuery
    interface seq<'T>
    interface IEnumerable
    interface IQueryable<'T>
    interface IQueryable
    interface IOrderedQueryable<'T>
    interface IOrderedQueryable
    interface IListSource
Public Class ObjectQuery(Of T)
Inherits ObjectQuery
Implements IEnumerable(Of T), IListSource, IOrderedQueryable(Of T), IQueryable(Of T)

Type Parameters

T

The entity type of the query.

Inheritance
ObjectQuery<T>ObjectQuery<T>ObjectQuery<T>ObjectQuery<T>
Derived
Implements

Examples

The example in this topic is based on the AdventureWorks Sales Model.

The example shows how to construct an instance of the ObjectQuery<T> class.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Call the constructor with a query for products and the ObjectContext.
    ObjectQuery<Product> productQuery1 =
        new ObjectQuery<Product>("Products", context);

    foreach (Product result in productQuery1)
        Console.WriteLine("Product Name: {0}", result.Name);

    string queryString =
        @"SELECT VALUE product FROM AdventureWorksEntities.Products AS product";

    // Call the constructor with the specified query and the ObjectContext.
    ObjectQuery<Product> productQuery2 =
        new ObjectQuery<Product>(queryString, context);

    foreach (Product result in productQuery2)
        Console.WriteLine("Product Name: {0}", result.Name);

    // Call the constructor with the specified query, the ObjectContext, 
    // and the NoTracking merge option.
    ObjectQuery<Product> productQuery3 =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    foreach (Product result in productQuery3)
        Console.WriteLine("Product Name: {0}", result.Name);
}
Using context As New AdventureWorksEntities()
    ' Call the constructor with a query for products and the ObjectContext. 
    Dim productQuery1 As New ObjectQuery(Of Product)("Products", context)

    For Each result As Product In productQuery1
        Console.WriteLine("Product Name: {0}", result.Name)
    Next

    Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product"

    ' Call the constructor with the specified query and the ObjectContext. 
    Dim productQuery2 As New ObjectQuery(Of Product)(queryString, context)

    For Each result As Product In productQuery2
        Console.WriteLine("Product Name: {0}", result.Name)
    Next

    ' Call the constructor with the specified query, the ObjectContext, 
    ' and the NoTracking merge option. 
    Dim productQuery3 As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking)

    For Each result As Product In productQuery3
        Console.WriteLine("Product Name: {0}", result.Name)
    Next
End Using

Remarks

The ObjectQuery<T> generic class represents a query that returns a collection of zero or more objects of a specific type. The ObjectContext must be defined before an object query can be executed. This context provides the connection and metadata information that is required to compose and execute the query. An object query is executed in the following scenarios:

  • When it is acted upon, such as during a foreach (C#) or For Each (Visual Basic) enumeration.

  • When it is assigned to fill a List<T> collection.

  • When the Execute method is explicitly called.

The query itself can be created by using an Entity SQL statement or a LINQ query, or it can be incrementally built by using the query builder methods included in the class. In every case, the result is a new ObjectQuery<T> instance that, when explicitly executed or enumerated over, will send the query to the data source for execution and return the results.

An ObjectQuery<T> is frequently of an entity type, but it can also be of DbDataRecord type, for projections to an anonymous type, or of a primitive type, such as an integer or string. For more information, see Object Queries.

Constructors

ObjectQuery<T>(String, ObjectContext) ObjectQuery<T>(String, ObjectContext) ObjectQuery<T>(String, ObjectContext) ObjectQuery<T>(String, ObjectContext)

Creates a new ObjectQuery<T> instance using the specified Entity SQL command as the initial query.

ObjectQuery<T>(String, ObjectContext, MergeOption) ObjectQuery<T>(String, ObjectContext, MergeOption) ObjectQuery<T>(String, ObjectContext, MergeOption) ObjectQuery<T>(String, ObjectContext, MergeOption)

Creates a new ObjectQuery<T> instance using the specified Entity SQL command as the initial query and the specified merge option.

Properties

CommandText CommandText CommandText CommandText

Returns the command text for the query.

(Inherited from ObjectQuery)
Context Context Context Context

Gets the object context associated with this object query.

(Inherited from ObjectQuery)
EnablePlanCaching EnablePlanCaching EnablePlanCaching EnablePlanCaching

Gets or sets a value that indicates whether the query plan should be cached.

(Inherited from ObjectQuery)
IListSource.ContainsListCollection IListSource.ContainsListCollection IListSource.ContainsListCollection IListSource.ContainsListCollection

Gets a value that indicates whether the collection returned by the query contains collection objects.

(Inherited from ObjectQuery)
IQueryable.ElementType IQueryable.ElementType IQueryable.ElementType IQueryable.ElementType

Gets the result element type for this query instance.

(Inherited from ObjectQuery)
IQueryable.Expression IQueryable.Expression IQueryable.Expression IQueryable.Expression

Gets the expression describing this query.

(Inherited from ObjectQuery)
IQueryable.Provider IQueryable.Provider IQueryable.Provider IQueryable.Provider

Gets the LINQ IQueryProvider associated with this query instance.

(Inherited from ObjectQuery)
MergeOption MergeOption MergeOption MergeOption

Gets or sets how objects returned from a query are added to the object context.

(Inherited from ObjectQuery)
Name Name Name Name

Gets or sets the name of this object query.

Parameters Parameters Parameters Parameters

Gets the parameter collection for this object query.

(Inherited from ObjectQuery)

Methods

Distinct() Distinct() Distinct() Distinct()

Limits the query to unique results.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Except(ObjectQuery<T>) Except(ObjectQuery<T>) Except(ObjectQuery<T>) Except(ObjectQuery<T>)

Limits the query results by excluding results based on the results of another object query.

Execute(MergeOption) Execute(MergeOption) Execute(MergeOption) Execute(MergeOption)

Executes the object query with the specified merge option.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetResultType() GetResultType() GetResultType() GetResultType()

Returns information about the result type of the query.

(Inherited from ObjectQuery)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
GroupBy(String, String, ObjectParameter[]) GroupBy(String, String, ObjectParameter[]) GroupBy(String, String, ObjectParameter[]) GroupBy(String, String, ObjectParameter[])

Groups the query results by the specified criteria.

Include(String) Include(String) Include(String) Include(String)

Specifies the related objects to include in the query results.

Intersect(ObjectQuery<T>) Intersect(ObjectQuery<T>) Intersect(ObjectQuery<T>) Intersect(ObjectQuery<T>)

Limits the query results by including only the results that exist in another object query.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OfType<TResultType>() OfType<TResultType>() OfType<TResultType>() OfType<TResultType>()

Limits the query to only results of a specific type.

OrderBy(String, ObjectParameter[]) OrderBy(String, ObjectParameter[]) OrderBy(String, ObjectParameter[]) OrderBy(String, ObjectParameter[])

Orders the query results by the specified criteria.

Select(String, ObjectParameter[]) Select(String, ObjectParameter[]) Select(String, ObjectParameter[]) Select(String, ObjectParameter[])

Limits the query results to only the properties that are defined in the specified projection.

SelectValue<TResultType>(String, ObjectParameter[]) SelectValue<TResultType>(String, ObjectParameter[]) SelectValue<TResultType>(String, ObjectParameter[]) SelectValue<TResultType>(String, ObjectParameter[])

Limits the query results to only the property specified in the projection.

Skip(String, String, ObjectParameter[]) Skip(String, String, ObjectParameter[]) Skip(String, String, ObjectParameter[]) Skip(String, String, ObjectParameter[])

Orders the query results by the specified criteria and skips a specified number of results.

Top(String, ObjectParameter[]) Top(String, ObjectParameter[]) Top(String, ObjectParameter[]) Top(String, ObjectParameter[])

Limits the query results to a specified number of items.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
ToTraceString() ToTraceString() ToTraceString() ToTraceString()

Returns the commands to execute against the data source.

(Inherited from ObjectQuery)
Union(ObjectQuery<T>) Union(ObjectQuery<T>) Union(ObjectQuery<T>) Union(ObjectQuery<T>)

Combines the results of the query with the results of another object query, without any duplicates.

UnionAll(ObjectQuery<T>) UnionAll(ObjectQuery<T>) UnionAll(ObjectQuery<T>) UnionAll(ObjectQuery<T>)

Combines the results of the query with the results of another object query, including all duplicates.

Where(String, ObjectParameter[]) Where(String, ObjectParameter[]) Where(String, ObjectParameter[]) Where(String, ObjectParameter[])

Limits the query to results that match specified filtering criteria.

Explicit Interface Implementations

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

(Inherited from ObjectQuery)
IEnumerable<T>.GetEnumerator() IEnumerable<T>.GetEnumerator() IEnumerable<T>.GetEnumerator() IEnumerable<T>.GetEnumerator()

Returns an enumerator that iterates through the results of the query.

IListSource.GetList() IListSource.GetList() IListSource.GetList() IListSource.GetList()

Returns the collection as an IList used for data binding.

(Inherited from ObjectQuery)

Extension Methods

CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>)

Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Cast<TResult>(IQueryable) Cast<TResult>(IQueryable) Cast<TResult>(IQueryable) Cast<TResult>(IQueryable)

Converts the elements of an IQueryable to the specified type.

OfType<TResult>(IQueryable) OfType<TResult>(IQueryable) OfType<TResult>(IQueryable) OfType<TResult>(IQueryable)

Filters the elements of an IQueryable based on a specified type.

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

Specifies a sort order.

Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>)

Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>)

Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>)

Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>)

Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName)

Returns a filtered collection of the child elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>)

Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>)

Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>)

Removes every node in the source collection from its parent node.

Applies to

See also