/// <summary>Limits the query to unique results.</summary>
 /// <returns>
 /// A new <see cref="T:System.Data.Entity.Core.Objects.ObjectQuery`1" /> instance that is equivalent to the original instance with SELECT DISTINCT applied.
 /// </returns>
 public ObjectQuery <T> Distinct()
 {
     if (ObjectQuery <T> .IsLinqQuery((ObjectQuery)this))
     {
         return((ObjectQuery <T>) this.Distinct <T>());
     }
     return(new ObjectQuery <T>(EntitySqlQueryBuilder.Distinct(this.QueryState)));
 }
 /// <summary>
 /// This query-builder method creates a new query whose results are all of
 /// the results of this query, plus all of the results of the other query,
 /// without duplicates (i.e., results are unique).
 /// </summary>
 /// <param name="query"> A query representing the results to add. </param>
 /// <returns> a new ObjectQuery instance. </returns>
 /// <exception cref="T:System.ArgumentNullException">If the query parameter is null.</exception>
 public ObjectQuery <T> Union(ObjectQuery <T> query)
 {
     Check.NotNull <ObjectQuery <T> >(query, nameof(query));
     if (ObjectQuery <T> .IsLinqQuery((ObjectQuery)this) || ObjectQuery <T> .IsLinqQuery((ObjectQuery)query))
     {
         return((ObjectQuery <T>) this.Union <T>((IEnumerable <T>)query));
     }
     return(new ObjectQuery <T>(EntitySqlQueryBuilder.Union(this.QueryState, query.QueryState)));
 }
        /// <summary>Limits the query to only results of a specific type.</summary>
        /// <returns>
        /// A new <see cref="T:System.Data.Entity.Core.Objects.ObjectQuery`1" /> instance that is equivalent to the original instance with OFTYPE applied.
        /// </returns>
        /// <typeparam name="TResultType">
        /// The type of the <see cref="T:System.Data.Entity.Core.Objects.ObjectResult`1" /> returned when the query is executed with the applied filter.
        /// </typeparam>
        /// <exception cref="T:System.Data.Entity.Core.EntitySqlException">The type specified is not valid.</exception>
        public ObjectQuery <TResultType> OfType <TResultType>()
        {
            if (ObjectQuery <T> .IsLinqQuery((ObjectQuery)this))
            {
                return((ObjectQuery <TResultType>)Queryable.OfType <TResultType>(this));
            }
            this.QueryState.ObjectContext.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResultType), Assembly.GetCallingAssembly());
            Type    type1 = typeof(TResultType);
            EdmType type2;

            if (!this.QueryState.ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.OSpace).TryGetType(type1.Name, type1.NestingNamespace() ?? string.Empty, out type2) || !Helper.IsEntityType(type2) && !Helper.IsComplexType(type2))
            {
                throw new EntitySqlException(Strings.ObjectQuery_QueryBuilder_InvalidResultType((object)typeof(TResultType).FullName));
            }
            return(new ObjectQuery <TResultType>(EntitySqlQueryBuilder.OfType(this.QueryState, type2, type1)));
        }