ObjectQuery.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataEntity / System / Data / Objects / ObjectQuery.cs / 1 / ObjectQuery.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner [....]
// @backupowner [....] 
//--------------------------------------------------------------------- 

using System; 
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel; 
using System.Globalization;
using System.Diagnostics; 
using System.Data; 
using System.Data.Common;
using System.Data.Common.EntitySql; 
using System.Data.Common.Utils;
using System.Data.Mapping;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees; 
using System.Data.Common.CommandTrees.Internal;
using System.Data.Objects.DataClasses; 
using System.Data.Objects.ELinq; 
using System.Data.Objects.Internal;
using System.Data.Common.QueryCache; 
using System.Data.EntityClient;
using System.Linq;
using System.Linq.Expressions;
 
namespace System.Data.Objects
{ 
    ///  
    ///   This class implements untyped queries at the object-layer.
    ///  
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
    public abstract class ObjectQuery : IEnumerable, IQueryable, IOrderedQueryable, IListSource
    {
        #region Private Instance Members 

        // ----------------- 
        // Instance Fields 
        // -----------------
 
        /// 
        ///   The underlying implementation of this ObjectQuery as provided by a concrete subclass
        ///   of ObjectQueryImplementation. Implementations currently exist for Entity-SQL- and Linq-to-Entities-based ObjectQueries.
        ///  
        private ObjectQueryState _state;
 
        ///  
        ///   The result type of the query - 'TResultType' expressed as an O-Space type usage. Cached here and
        ///   only instantiated if the  method is called. 
        /// 
        private TypeUsage _resultType;

        #endregion 

        #region Internal Constructors 
 
        // --------------------
        // Internal Constructors 
        // --------------------

        /// 
        ///   The common constructor. 
        /// 
        ///  
        ///   The underlying implementation of this ObjectQuery 
        /// 
        ///  
        ///   A new ObjectQuery instance.
        /// 
        internal ObjectQuery(ObjectQueryState queryState)
        { 
            Debug.Assert(queryState != null, "ObjectQuery state cannot be null");
 
            // Set the query state. 
            this._state = queryState;
 
            // Flag that indicates if this ObjectQuery query plan has to be cached
            // Future Enhancement: Read the value of this setting from a 'global' configuration such as an .exe.config section
            this._state.PlanCachingEnabled = true;
        } 

        #endregion 
 
        #region Internal Properties
 
        /// 
        /// Gets an untyped instantiation of the underlying ObjectQueryState that implements this ObjectQuery.
        /// 
        internal ObjectQueryState QueryState { get { return this._state; } } 

        #endregion 
 
        #region IQueryable implementation
 
        /// 
        /// Gets the result element type for this query instance.
        /// 
        Type IQueryable.ElementType 
        {
            get { return this._state.ElementType;; } 
        } 

        ///  
        /// Gets the expression describing this query. For queries built using
        /// LINQ builder patterns, returns a full LINQ expression tree; otherwise,
        /// returns a constant expression wrapping this query. Note that the
        /// default expression is not cached. This allows us to differentiate 
        /// between LINQ and Entity-SQL queries.
        ///  
        System.Linq.Expressions.Expression IQueryable.Expression 
        {
            get 
            {
                Expression retExpr;
                if (!this._state.TryGetExpression(out retExpr))
                { 
                    retExpr = Expression.Constant(this);
                } 
 
                return retExpr;
            } 
        }

        /// 
        /// Gets the IQueryProvider associated with this query instance. 
        /// 
        IQueryProvider IQueryable.Provider { get { return this.Context.Provider; } } 
 
        #endregion
 
        #region Public Properties

        // ----------
        // Properties 
        // ----------
 
        // ---------------------- 
        // IListSource  Properties
        // ---------------------- 
        /// 
        ///   IListSource.ContainsListCollection implementation. Always returns true.
        /// 
        bool IListSource.ContainsListCollection 
        {
            get 
            { 
                return false; // this means that the IList we return is the one which contains our actual data, it is not a collection
            } 
        }

        /// 
        /// Gets the Command Text (if any) for this ObjectQuery. 
        /// 
        public string CommandText 
        { 
            get
            { 
                string commandText;
                if (!_state.TryGetCommandText(out commandText))
                {
                    return String.Empty; 
                }
 
                Debug.Assert(commandText != null && commandText.Length != 0, "Invalid Command Text returned"); 
                return commandText;
            } 
        }

        /// 
        ///   The context for the query, which includes the connection, cache and 
        ///   metadata. Note that only the connection property is mutable and must be
        ///   set before a query can be executed. 
        ///  
        public ObjectContext Context
        { 
            get
            {
                return this._state.ObjectContext;
            } 
        }
 
        ///  
        ///   Allows optional control over how queried results interact with the object state manager.
        ///  
        public MergeOption MergeOption
        {
            get
            { 
                return this._state.EffectiveMergeOption;
            } 
 
            set
            { 
                EntityUtil.CheckArgumentMergeOption(value);
                this._state.UserSpecifiedMergeOption = value;
            }
        } 

        ///  
        ///   The parameter collection for this query. 
        /// 
        public ObjectParameterCollection Parameters 
        {
            get
            {
                return this._state.EnsureParameters(); 
            }
        } 
 
        /// 
        ///   Defines if the query plan should be cached 
        /// 
        public bool EnablePlanCaching
        {
            get 
            {
                return this._state.PlanCachingEnabled; 
            } 

            set 
            {
                this._state.PlanCachingEnabled = value;
            }
        } 

        #endregion 
 
        #region Public Methods
 
        // --------------
        // Public Methods
        // --------------
 
        // ----------------------
        // IListSource  method 
        // ---------------------- 
        /// 
        ///   IListSource.GetList implementation 
        /// 
        /// 
        ///   IList interface over the data to bind
        ///  
        IList IListSource.GetList()
        { 
            return this.GetIListSourceListInternal(); 
        }
 
        /// 
        ///   Get the provider-specific command text used to execute this query
        /// 
        ///  
        [Browsable(false)]
        public string ToTraceString() 
        { 
            return this._state.GetExecutionPlan(null).ToTraceString();
        } 

        /// 
        ///   This method returns information about the result type of the ObjectQuery.
        ///  
        /// 
        ///   The TypeMetadata that describes the shape of the query results. 
        ///  
        public TypeUsage GetResultType ()
        { 
            Context.EnsureMetadata();
            if (null == this._resultType)
            {
                // Retrieve the result type from the implementation, in terms of C-Space. 
                TypeUsage cSpaceQueryResultType = this._state.ResultType;
 
                // Determine the 'TResultType' equivalent type usage based on the mapped O-Space type. 
                // If the result type of the query is a collection[something], then
                // extract out the 'something' (element type) and use that. This 
                // is the equivalent of saying the result type is T, rather than
                // IEnumerable, which aligns with users' expectations.
                TypeUsage tResultType;
                if (!TypeHelpers.TryGetCollectionElementType(cSpaceQueryResultType, out tResultType)) 
                {
                    tResultType = cSpaceQueryResultType; 
                } 

                // Map the C-space result type to O-space. 
                tResultType = this._state.ObjectContext.Perspective.MetadataWorkspace.GetOSpaceTypeUsage(tResultType);
                if (null == tResultType)
                {
                    throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.ObjectQuery_UnableToMapResultType); 
                }
 
                this._resultType = tResultType; 
            }
 
            return this._resultType;
        }

        ///  
        ///   This method allows explicit query evaluation with a specified merge
        ///   option which will override the merge option property. 
        ///  
        /// 
        ///   The MergeOption to use when executing the query. 
        /// 
        /// 
        ///   An enumerable for the ObjectQuery results.
        ///  
        public ObjectResult Execute(MergeOption mergeOption)
        { 
            EntityUtil.CheckArgumentMergeOption(mergeOption); 
            return this.ExecuteInternal(mergeOption);
        } 

        #region IEnumerable implementation

        IEnumerator IEnumerable.GetEnumerator() 
        {
            return this.GetEnumeratorInternal(); 
        } 

        #endregion 

        #endregion

        #region Internal Methods 

        ///  
        /// This method returns successfully only if the query is valid enough to attempt execution - 
        /// that is, calling  will not result in an exception caused during
        /// compilation of this query into a provider command. 
        /// 
        internal void Validate()
        {
            this._state.GetExecutionPlan(null); 
        }
 
        internal abstract IEnumerator GetEnumeratorInternal(); 
        internal abstract IList GetIListSourceListInternal();
        internal abstract ObjectResult ExecuteInternal(MergeOption mergeOption); 
        #endregion
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner [....]
// @backupowner [....] 
//--------------------------------------------------------------------- 

using System; 
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel; 
using System.Globalization;
using System.Diagnostics; 
using System.Data; 
using System.Data.Common;
using System.Data.Common.EntitySql; 
using System.Data.Common.Utils;
using System.Data.Mapping;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees; 
using System.Data.Common.CommandTrees.Internal;
using System.Data.Objects.DataClasses; 
using System.Data.Objects.ELinq; 
using System.Data.Objects.Internal;
using System.Data.Common.QueryCache; 
using System.Data.EntityClient;
using System.Linq;
using System.Linq.Expressions;
 
namespace System.Data.Objects
{ 
    ///  
    ///   This class implements untyped queries at the object-layer.
    ///  
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
    public abstract class ObjectQuery : IEnumerable, IQueryable, IOrderedQueryable, IListSource
    {
        #region Private Instance Members 

        // ----------------- 
        // Instance Fields 
        // -----------------
 
        /// 
        ///   The underlying implementation of this ObjectQuery as provided by a concrete subclass
        ///   of ObjectQueryImplementation. Implementations currently exist for Entity-SQL- and Linq-to-Entities-based ObjectQueries.
        ///  
        private ObjectQueryState _state;
 
        ///  
        ///   The result type of the query - 'TResultType' expressed as an O-Space type usage. Cached here and
        ///   only instantiated if the  method is called. 
        /// 
        private TypeUsage _resultType;

        #endregion 

        #region Internal Constructors 
 
        // --------------------
        // Internal Constructors 
        // --------------------

        /// 
        ///   The common constructor. 
        /// 
        ///  
        ///   The underlying implementation of this ObjectQuery 
        /// 
        ///  
        ///   A new ObjectQuery instance.
        /// 
        internal ObjectQuery(ObjectQueryState queryState)
        { 
            Debug.Assert(queryState != null, "ObjectQuery state cannot be null");
 
            // Set the query state. 
            this._state = queryState;
 
            // Flag that indicates if this ObjectQuery query plan has to be cached
            // Future Enhancement: Read the value of this setting from a 'global' configuration such as an .exe.config section
            this._state.PlanCachingEnabled = true;
        } 

        #endregion 
 
        #region Internal Properties
 
        /// 
        /// Gets an untyped instantiation of the underlying ObjectQueryState that implements this ObjectQuery.
        /// 
        internal ObjectQueryState QueryState { get { return this._state; } } 

        #endregion 
 
        #region IQueryable implementation
 
        /// 
        /// Gets the result element type for this query instance.
        /// 
        Type IQueryable.ElementType 
        {
            get { return this._state.ElementType;; } 
        } 

        ///  
        /// Gets the expression describing this query. For queries built using
        /// LINQ builder patterns, returns a full LINQ expression tree; otherwise,
        /// returns a constant expression wrapping this query. Note that the
        /// default expression is not cached. This allows us to differentiate 
        /// between LINQ and Entity-SQL queries.
        ///  
        System.Linq.Expressions.Expression IQueryable.Expression 
        {
            get 
            {
                Expression retExpr;
                if (!this._state.TryGetExpression(out retExpr))
                { 
                    retExpr = Expression.Constant(this);
                } 
 
                return retExpr;
            } 
        }

        /// 
        /// Gets the IQueryProvider associated with this query instance. 
        /// 
        IQueryProvider IQueryable.Provider { get { return this.Context.Provider; } } 
 
        #endregion
 
        #region Public Properties

        // ----------
        // Properties 
        // ----------
 
        // ---------------------- 
        // IListSource  Properties
        // ---------------------- 
        /// 
        ///   IListSource.ContainsListCollection implementation. Always returns true.
        /// 
        bool IListSource.ContainsListCollection 
        {
            get 
            { 
                return false; // this means that the IList we return is the one which contains our actual data, it is not a collection
            } 
        }

        /// 
        /// Gets the Command Text (if any) for this ObjectQuery. 
        /// 
        public string CommandText 
        { 
            get
            { 
                string commandText;
                if (!_state.TryGetCommandText(out commandText))
                {
                    return String.Empty; 
                }
 
                Debug.Assert(commandText != null && commandText.Length != 0, "Invalid Command Text returned"); 
                return commandText;
            } 
        }

        /// 
        ///   The context for the query, which includes the connection, cache and 
        ///   metadata. Note that only the connection property is mutable and must be
        ///   set before a query can be executed. 
        ///  
        public ObjectContext Context
        { 
            get
            {
                return this._state.ObjectContext;
            } 
        }
 
        ///  
        ///   Allows optional control over how queried results interact with the object state manager.
        ///  
        public MergeOption MergeOption
        {
            get
            { 
                return this._state.EffectiveMergeOption;
            } 
 
            set
            { 
                EntityUtil.CheckArgumentMergeOption(value);
                this._state.UserSpecifiedMergeOption = value;
            }
        } 

        ///  
        ///   The parameter collection for this query. 
        /// 
        public ObjectParameterCollection Parameters 
        {
            get
            {
                return this._state.EnsureParameters(); 
            }
        } 
 
        /// 
        ///   Defines if the query plan should be cached 
        /// 
        public bool EnablePlanCaching
        {
            get 
            {
                return this._state.PlanCachingEnabled; 
            } 

            set 
            {
                this._state.PlanCachingEnabled = value;
            }
        } 

        #endregion 
 
        #region Public Methods
 
        // --------------
        // Public Methods
        // --------------
 
        // ----------------------
        // IListSource  method 
        // ---------------------- 
        /// 
        ///   IListSource.GetList implementation 
        /// 
        /// 
        ///   IList interface over the data to bind
        ///  
        IList IListSource.GetList()
        { 
            return this.GetIListSourceListInternal(); 
        }
 
        /// 
        ///   Get the provider-specific command text used to execute this query
        /// 
        ///  
        [Browsable(false)]
        public string ToTraceString() 
        { 
            return this._state.GetExecutionPlan(null).ToTraceString();
        } 

        /// 
        ///   This method returns information about the result type of the ObjectQuery.
        ///  
        /// 
        ///   The TypeMetadata that describes the shape of the query results. 
        ///  
        public TypeUsage GetResultType ()
        { 
            Context.EnsureMetadata();
            if (null == this._resultType)
            {
                // Retrieve the result type from the implementation, in terms of C-Space. 
                TypeUsage cSpaceQueryResultType = this._state.ResultType;
 
                // Determine the 'TResultType' equivalent type usage based on the mapped O-Space type. 
                // If the result type of the query is a collection[something], then
                // extract out the 'something' (element type) and use that. This 
                // is the equivalent of saying the result type is T, rather than
                // IEnumerable, which aligns with users' expectations.
                TypeUsage tResultType;
                if (!TypeHelpers.TryGetCollectionElementType(cSpaceQueryResultType, out tResultType)) 
                {
                    tResultType = cSpaceQueryResultType; 
                } 

                // Map the C-space result type to O-space. 
                tResultType = this._state.ObjectContext.Perspective.MetadataWorkspace.GetOSpaceTypeUsage(tResultType);
                if (null == tResultType)
                {
                    throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.ObjectQuery_UnableToMapResultType); 
                }
 
                this._resultType = tResultType; 
            }
 
            return this._resultType;
        }

        ///  
        ///   This method allows explicit query evaluation with a specified merge
        ///   option which will override the merge option property. 
        ///  
        /// 
        ///   The MergeOption to use when executing the query. 
        /// 
        /// 
        ///   An enumerable for the ObjectQuery results.
        ///  
        public ObjectResult Execute(MergeOption mergeOption)
        { 
            EntityUtil.CheckArgumentMergeOption(mergeOption); 
            return this.ExecuteInternal(mergeOption);
        } 

        #region IEnumerable implementation

        IEnumerator IEnumerable.GetEnumerator() 
        {
            return this.GetEnumeratorInternal(); 
        } 

        #endregion 

        #endregion

        #region Internal Methods 

        ///  
        /// This method returns successfully only if the query is valid enough to attempt execution - 
        /// that is, calling  will not result in an exception caused during
        /// compilation of this query into a provider command. 
        /// 
        internal void Validate()
        {
            this._state.GetExecutionPlan(null); 
        }
 
        internal abstract IEnumerator GetEnumeratorInternal(); 
        internal abstract IList GetIListSourceListInternal();
        internal abstract ObjectResult ExecuteInternal(MergeOption mergeOption); 
        #endregion
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK