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

定義

表示在指定的物件內容中針對概念模型的具類型查詢。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>ObjectQuery<T>ObjectQuery<T>ObjectQuery<T>
衍生
實作

範例

本主題中的範例根據AdventureWorks Sales ModelThe 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> 泛型類別 (Class) 表示傳回零個或多個特定型別物件之集合的查詢。The ObjectQuery<T> generic class represents a query that returns a collection of zero or more objects of a specific type. 您必須先定義 ObjectContext,然後才能執行物件查詢。The ObjectContext must be defined before an object query can be executed. 此內容提供了撰寫及執行查詢所需的連接和中繼資料 (Metadata) 資訊。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 陳述式 (Statement) 或 LINQ 查詢來建立查詢本身,也可以使用此類別所包含的查詢產生器方法,以累加方式建置 (Build) 此查詢。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> 通常是實體 (Entity) 類型,但是它也可以是 DbDataRecord 型別 (匿名型別的投影) 或基本型別 (Primitive Type) (例如整數或字串)。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) ObjectQuery<T>(String, ObjectContext) ObjectQuery<T>(String, ObjectContext) 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) ObjectQuery<T>(String, ObjectContext, MergeOption) ObjectQuery<T>(String, ObjectContext, MergeOption) 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 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

取得與這個查詢執行個體相關聯的 LINQ IQueryProviderGets 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)

方法

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

取得目前執行個體的 TypeGets 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()

建立目前 Object 的淺層複本 (Shallow Copy)。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.

明確介面實作

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

傳回集合當做用於資料繫結 (Data Binding) 的 IListReturns 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>)

傳回DataTable,其中包含副本DataRow物件,指定輸入IEnumerable<T>物件其中泛型參數TDataRowReturns 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)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

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

根據指定的型別來篩選 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)

將轉換IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

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

IQueryable 的項目轉換為指定的型別。Converts the elements of an IQueryable to the specified type.

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

篩選的項目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. 集合中只會包含具有相符之 XName 的項目。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. 集合中只會包含具有相符之 XName 的項目。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. 集合中只會包含具有相符之 XName 的項目。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.

適用於

另請參閱