/// <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 (IsLinqQuery(this)) { return((ObjectQuery <TResultType>)Queryable.OfType <TResultType>(this)); } // SQLPUDT 484477: Make sure TResultType is loaded. QueryState.ObjectContext.MetadataWorkspace.ImplicitLoadAssemblyForType(typeof(TResultType), Assembly.GetCallingAssembly()); // Retrieve the O-Space type metadata for the result type specified. If no // metadata can be found for the specified type, fail. Otherwise, if the // type metadata found for TResultType is not either an EntityType or a // ComplexType, fail - OfType() is not a valid operation on scalars, // enumerations, collections, etc. var clrOfType = typeof(TResultType); EdmType ofType; if (!QueryState.ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.OSpace).TryGetType( clrOfType.Name, clrOfType.NestingNamespace() ?? string.Empty, out ofType) || !(Helper.IsEntityType(ofType) || Helper.IsComplexType(ofType))) { var message = Strings.ObjectQuery_QueryBuilder_InvalidResultType(typeof(TResultType).FullName); throw new EntitySqlException(message); } return(new ObjectQuery <TResultType>(EntitySqlQueryBuilder.OfType(QueryState, ofType, clrOfType))); }
/// <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))); }