ObjectQuery<T> 类

定义

表示在给定对象上下文中对概念模型的类型化查询。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)

类型参数

T

查询的实体类型。The entity type of the query.

继承
ObjectQuery<T>
派生
实现

示例

本主题中的示例基于AdventureWorks 销售模型The example in this topic is based on the AdventureWorks Sales Model.

该示例演示如何构造 ObjectQuery<T> 类的实例。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

注解

ObjectQuery<T> 泛型类表示一个查询,该查询返回由零个或多个特定类型的对象组成的集合。The ObjectQuery<T> generic class represents a query that returns a collection of zero or more objects of a specific type. 在执行对象查询之前,必须定义 ObjectContextThe 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:

  • 对其执行操作时,例如 foreach (C#) 或 For Each (Visual Basic) 枚举期间。When it is acted upon, such as during a foreach (C#) or For Each (Visual Basic) enumeration.

  • 分配用于填充 List<T> 集合时。When it is assigned to fill a List<T> collection.

  • 显式调用 Execute 方法时。When the Execute method is explicitly called.

查询本身可以使用一个 Entity SQLEntity SQL 语句或 LINQ 查询进行创建,也可以使用类中包含的查询生成器方法以增量方式生成。The query itself can be created by using an Entity SQLEntity SQL statement or a LINQ query, or it can be incrementally built by using the query builder methods included in the class. 在每种情况下,结果都为一个新 ObjectQuery<T> 实例,该实例在显式执行或对其进行枚举时,会将查询发送到数据源进行执行并返回结果。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.

ObjectQuery<T> 通常是实体类型,也可以是 DbDataRecord 类型(用于匿名类型的投影)或基元类型(如整数或字符串)。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.

构造函数

ObjectQuery<T>(String, ObjectContext)

使用指定的 Entity SQLEntity SQL 命令作为初始查询来创建新的 ObjectQuery<T> 实例。Creates a new ObjectQuery<T> instance using the specified Entity SQLEntity SQL command as the initial query.

ObjectQuery<T>(String, ObjectContext, MergeOption)

使用指定的 Entity SQLEntity SQL 命令(作为初始查询)和指定的合并选项来创建新的 ObjectQuery<T> 实例。Creates a new ObjectQuery<T> instance using the specified Entity SQLEntity SQL command as the initial query and the specified merge option.

属性

CommandText

返回查询的命令文本。Returns the command text for the query.

(继承自 ObjectQuery)
Context

获取与此对象查询相关联的对象上下文。Gets the object context associated with this object query.

(继承自 ObjectQuery)
EnablePlanCaching

获取或设置一个值,该值指示是否应缓存查询计划。Gets or sets a value that indicates whether the query plan should be cached.

(继承自 ObjectQuery)
MergeOption

获取或设置从查询返回的对象添加到对象上下文的方式。Gets or sets how objects returned from a query are added to the object context.

(继承自 ObjectQuery)
Name

获取或设置此对象查询的名称。Gets or sets the name of this object query.

Parameters

获取此对象查询的参数集合。Gets the parameter collection for this object query.

(继承自 ObjectQuery)

方法

Distinct()

将查询限制为唯一结果。Limits the query to unique results.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Except(ObjectQuery<T>)

通过根据另一个对象查询的结果排除结果的方式限制查询结果。Limits the query results by excluding results based on the results of another object query.

Execute(MergeOption)

使用指定的合并选项执行对象查询。Executes the object query with the specified merge option.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetResultType()

返回有关查询的结果类型的信息。Returns information about the result type of the query.

(继承自 ObjectQuery)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GroupBy(String, String, ObjectParameter[])

按指定的条件对查询结果进行分组。Groups the query results by the specified criteria.

Include(String)

指定要包括在查询结果中的相关对象。Specifies the related objects to include in the query results.

Intersect(ObjectQuery<T>)

通过仅包含另一个对象查询中存在的结果的方式限制查询结果。Limits the query results by including only the results that exist in another object query.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OfType<TResultType>()

将查询限制为仅包括特定类型的结果。Limits the query to only results of a specific type.

OrderBy(String, ObjectParameter[])

按指定条件对查询结果进行排序。Orders the query results by the specified criteria.

Select(String, ObjectParameter[])

将查询结果限制为仅包含在指定投影中定义的属性。Limits the query results to only the properties that are defined in the specified projection.

SelectValue<TResultType>(String, ObjectParameter[])

将查询结果限制为仅包含投影中指定的属性。Limits the query results to only the property specified in the projection.

Skip(String, String, ObjectParameter[])

按指定条件对查询结果进行排序并跳过指定数目的结果。Orders the query results by the specified criteria and skips a specified number of results.

Top(String, ObjectParameter[])

将查询结果限制为指定的项数。Limits the query results to a specified number of items.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
ToTraceString()

返回对数据源执行的命令。Returns the commands to execute against the data source.

(继承自 ObjectQuery)
Union(ObjectQuery<T>)

将查询结果与另一个对象查询的结果进行组合(不包括重复项)。Combines the results of the query with the results of another object query, without any duplicates.

UnionAll(ObjectQuery<T>)

将查询结果与另一个对象查询的结果进行组合(包括所有重复项)。Combines the results of the query with the results of another object query, including all duplicates.

Where(String, ObjectParameter[])

将查询限制为包含与指定筛选条件匹配的结果。Limits the query to results that match specified filtering criteria.

显式界面实现

IEnumerable.GetEnumerator()

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

(继承自 ObjectQuery)
IEnumerable<T>.GetEnumerator()

返回一个循环访问查询结果的枚举器。Returns an enumerator that iterates through the results of the query.

IListSource.ContainsListCollection

获取一个值,该值指示查询返回的集合是否包含集合对象。Gets a value that indicates whether the collection returned by the query contains collection objects.

(继承自 ObjectQuery)
IListSource.GetList()

IList 形式返回一个集合,用于数据绑定。Returns the collection as an IList used for data binding.

(继承自 ObjectQuery)
IQueryable.ElementType

获取此查询实例的结果元素类型。Gets the result element type for this query instance.

(继承自 ObjectQuery)
IQueryable.Expression

获取描述此查询的表达式。Gets the expression describing this query.

(继承自 ObjectQuery)
IQueryable.Provider

获取与此查询实例关联的 LINQ IQueryProviderGets the LINQ IQueryProvider associated with this query instance.

(继承自 ObjectQuery)

扩展方法

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns 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)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies 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)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Cast<TResult>(IQueryable)

IQueryable 的元素转换为指定的类型。Converts the elements of an IQueryable to the specified type.

OfType<TResult>(IQueryable)

根据指定类型筛选 IQueryable 的元素。Filters the elements of an IQueryable based on a specified type.

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

指定排序顺序。Specifies a sort order.

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)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

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)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于

另请参阅