/// <summary> /// Wraps the IEnumerable in a DbDataReader, having one column for each "scalar" property of the type T. /// The collection will be enumerated as the client calls IDataReader.Read(). /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collection"></param> /// <returns></returns> public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, bool exposeNullableColumns, bool flattenRelatedObjects) { var options = new EntityDataReaderOptions(exposeNullableColumns, flattenRelatedObjects, true, false); return(new EntityDataReader <T>(collection, options, null)); }
/// <summary> /// Wraps the collection in a DataReader, but also includes columns for the key attributes of related Entities. /// </summary> /// <typeparam name="T">The element type of the collection.</typeparam> /// <param name="collection">A collection to wrap in a DataReader</param> /// <param name="cx">The Entity Framework ObjectContext, used for metadata access</param> /// <returns>A DbDataReader wrapping the collection.</returns> public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, ObjectContext context) where T : EntityObject { EntityDataReaderOptions options = EntityDataReaderOptions.Default; options.RecreateForeignKeysForEntityFrameworkEntities = true; return(new EntityDataReader <T>(collection, options, context)); }
/// <summary> /// Enumerates the collection and copies the data into a DataTable. /// </summary> /// <typeparam name="T">The element type of the collection.</typeparam> /// <param name="collection">The collection to copy to a DataTable</param> /// <returns>A DataTable containing the scalar projection of the collection.</returns> public static System.Data.DataTable ToDataTable <T>(this IEnumerable <T> collection) { var t = new System.Data.DataTable(); t.Locale = CultureInfo.CurrentCulture; t.TableName = typeof(T).Name; EntityDataReaderOptions options = EntityDataReaderOptions.Default; options.ExposeNullableTypes = false; var dr = new EntityDataReader <T>(collection, options); t.Load(dr); return(t); }
/// <summary> /// Wraps the IEnumerable in a DbDataReader, having one column for each "scalar" property of the type T. /// The collection will be enumerated as the client calls IDataReader.Read(). /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collection"></param> /// <returns></returns> public static IDataReader AsDataReader <T>(this IEnumerable <T> collection) { //For anonymous type projections default to flattening related objects and not prefixing columns //The reason being that if the programmer has taken control of the projection, the default should //be to expose everying in the projection and not mess with the names. if (typeof(T).IsDefined(typeof(CompilerGeneratedAttribute), false)) { EntityDataReaderOptions options = EntityDataReaderOptions.Default; options.FlattenRelatedObjects = true; options.PrefixRelatedObjectColumns = false; return(new EntityDataReader <T>(collection, options)); } return(new EntityDataReader <T>(collection)); }
/// <summary> /// Wraps the collection in a DataReader, but also includes columns for the key attributes of related Entities. /// </summary> /// <typeparam name="T">The element type of the collectin.</typeparam> /// <param name="collection">A collection to wrap in a DataReader</param> /// <param name="detachObjects">Option to detach each object in the collection from the ObjectContext. This can reduce memory usage for queries returning large numbers of objects.</param> /// <param name="prefixReladObjectColumns">If True, qualify the related object keys, if False don't</param> /// <returns>A DbDataReader wrapping the collection.</returns> /// <summary> public static IDataReader AsDataReader <T>(this IEnumerable <T> collection, ObjectContext context, bool detachObjects, bool prefixRelatedObjectColumns) where T : EntityObject { EntityDataReaderOptions options = EntityDataReaderOptions.Default; options.RecreateForeignKeysForEntityFrameworkEntities = true; options.PrefixRelatedObjectColumns = prefixRelatedObjectColumns; if (detachObjects) { return(new EntityDataReader <T>(collection.DetachAllFrom(context), options, context)); } return(new EntityDataReader <T>(collection, options, context)); }
/// <summary> /// Enumerates the collection and copies the data into a DataTable, but also includes columns for the key attributes of related Entities. /// </summary> /// <typeparam name="T">The element type of the collection.</typeparam> /// <param name="collection">The collection to copy to a DataTable</param> /// <param name="cx">The Entity Framework ObjectContext, used for metadata access</param> /// <returns>A DataTable containing the scalar projection of the collection.</returns> public static System.Data.DataTable ToDataTable <T>(this IEnumerable <T> collection, ObjectContext context) where T : EntityObject { var t = new System.Data.DataTable(); t.Locale = CultureInfo.CurrentCulture; t.TableName = typeof(T).Name; EntityDataReaderOptions options = EntityDataReaderOptions.Default; options.RecreateForeignKeysForEntityFrameworkEntities = true; var dr = new EntityDataReader <T>(collection, options, context); t.Load(dr); return(t); }
public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options, ObjectContext objectContext) { enumerator = col.GetEnumerator(); this.options = options; if (options.RecreateForeignKeysForEntityFrameworkEntities && objectContext == null) { throw new ArgumentException( "If RecreateForeignKeysForEntityFrameworkEntities=true then objectContext is required"); } //done without a lock, so we risk running twice if (scalarAttributes == null) { scalarAttributes = DiscoverScalarAttributes(typeof(T)); } if (options.FlattenRelatedObjects && scalarAttributesPlusRelatedObjectScalarAttributes == null) { List <Attribute> atts = DiscoverRelatedObjectScalarAttributes(typeof(T)); scalarAttributesPlusRelatedObjectScalarAttributes = atts.Concat(scalarAttributes).ToList(); } if (options.RecreateForeignKeysForEntityFrameworkEntities && scalarAttributesPlusRelatedObjectKeyAttributes == null) { List <Attribute> atts = DiscoverRelatedObjectKeyAttributes(typeof(T), objectContext); scalarAttributesPlusRelatedObjectKeyAttributes = atts.Concat(scalarAttributes).ToList(); } if (options.FlattenRelatedObjects) { attributes = scalarAttributesPlusRelatedObjectScalarAttributes; } else if (objectContext != null) { attributes = scalarAttributesPlusRelatedObjectKeyAttributes; } else { attributes = scalarAttributes; } }
public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options) : this(col, options, null) { }