/// <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>
        /// 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.
        /// </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 DataTable ToDataTable <T>(this IEnumerable <T> collection)
        {
            DataTable t = new DataTable();

            t.Locale    = System.Globalization.CultureInfo.CurrentCulture;
            t.TableName = typeof(T).Name;
            EntityDataReaderOptions options = EntityDataReaderOptions.Default;

            options.ExposeNullableTypes = false;
            EntityDataReader <T> dr = new EntityDataReader <T>(collection, options);

            t.Load(dr);
            return(t);
        }
        /// <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 DataTable ToDataTable <T>(this IEnumerable <T> collection, ObjectContext context) where T : EntityObject
        {
            DataTable t = new DataTable();

            t.Locale    = System.Globalization.CultureInfo.CurrentCulture;
            t.TableName = typeof(T).Name;

            EntityDataReaderOptions options = EntityDataReaderOptions.Default;

            options.RecreateForeignKeysForEntityFrameworkEntities = true;

            EntityDataReader <T> dr = new EntityDataReader <T>(collection, options, context);

            t.Load(dr);
            return(t);
        }
        public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options, ObjectContext objectContext)
        {
            this.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)
            {
                var atts = DiscoverRelatedObjectScalarAttributes(typeof(T));
                scalarAttributesPlusRelatedObjectScalarAttributes = atts.Concat(scalarAttributes).ToList();
            }
            if (options.RecreateForeignKeysForEntityFrameworkEntities && scalarAttributesPlusRelatedObjectKeyAttributes == null)
            {
                var atts = DiscoverRelatedObjectKeyAttributes(typeof(T), objectContext);
                scalarAttributesPlusRelatedObjectKeyAttributes = atts.Concat(scalarAttributes).ToList();
            }

            if (options.FlattenRelatedObjects)
            {
                attributes = scalarAttributesPlusRelatedObjectScalarAttributes;
            }
            else if (objectContext != null)
            {
                attributes = scalarAttributesPlusRelatedObjectKeyAttributes;
            }
            else
            {
                attributes = scalarAttributes;
            }
        }
        /// <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)
        {
            EntityDataReaderOptions options = new EntityDataReaderOptions(exposeNullableColumns, flattenRelatedObjects, true, false);

            return(new EntityDataReader <T>(collection, options, null));
        }
 public EntityDataReader(IEnumerable <T> col, EntityDataReaderOptions options)
     : this(col, options, null)
 {
 }