public static void Fetch <T>(this IQueryable <T> querable, Action <FetchResult <T> > action, FetchOptions <T> options) where T : class, new()
        {
            var dbContext = querable.GetDbContext();
            var sqlQuery  = SqlBuilder.Parse(querable.ToQueryString());

            if (options.InputColumns != null || options.IgnoreColumns != null)
            {
                var tableMapping = dbContext.GetTableMapping(typeof(T));
                IEnumerable <string> columnNames = options.InputColumns != null?options.InputColumns.GetObjectProperties() : tableMapping.GetColumns(true);

                IEnumerable <string> columnsToFetch = CommonUtil.FormatColumns(columnNames.Where(o => !options.IgnoreColumns.GetObjectProperties().Contains(o)));
                sqlQuery.SelectColumns(columnsToFetch);
            }
            var command = dbContext.Database.GetDbConnection().CreateCommand();

            command.CommandText = sqlQuery.Sql;
            command.Parameters.AddRange(sqlQuery.Parameters.ToArray());
            var reader = command.ExecuteReader();

            List <PropertyInfo> propertySetters = new List <PropertyInfo>();
            var entityType = typeof(T);

            for (int i = 0; i < reader.FieldCount; i++)
            {
                propertySetters.Add(entityType.GetProperty(reader.GetName(i)));
            }
            //Read data
            int batch      = 1;
            int count      = 0;
            int totalCount = 0;
            var entities   = new List <T>();

            while (reader.Read())
            {
                var entity = new T();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    var value = reader.GetValue(i);
                    if (value == DBNull.Value)
                    {
                        value = null;
                    }
                    propertySetters[i].SetValue(entity, value);
                }
                entities.Add(entity);
                count++;
                totalCount++;
                if (count == options.BatchSize)
                {
                    action(new FetchResult <T> {
                        Results = entities, Batch = batch
                    });
                    entities.Clear();
                    count = 0;
                    batch++;
                }
            }

            if (entities.Count > 0)
            {
                action(new FetchResult <T> {
                    Results = entities, Batch = batch
                });
            }
            //close the DataReader
            reader.Close();
        }
Beispiel #2
0
        public static void Fetch <T>(this IQueryable <T> querable, Action <FetchResult <T> > action, FetchOptions options) where T : class, new()
        {
            var dbContext    = GetDbContextFromIQuerable(querable);
            var dbConnection = dbContext.GetSqlConnection();

            //Open datbase connection
            if (dbConnection.State == ConnectionState.Closed)
            {
                dbConnection.Open();
            }
            var command = new SqlCommand(querable.ToQueryString(), dbConnection);
            var reader  = command.ExecuteReader();

            List <PropertyInfo> propertySetters = new List <PropertyInfo>();
            var entityType = typeof(T);

            for (int i = 0; i < reader.FieldCount; i++)
            {
                propertySetters.Add(entityType.GetProperty(reader.GetName(i)));
            }
            //Read data
            int batch      = 1;
            int count      = 0;
            int totalCount = 0;
            var entities   = new List <T>();

            while (reader.Read())
            {
                var entity = new T();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    var value = reader.GetValue(i);
                    if (value == DBNull.Value)
                    {
                        value = null;
                    }
                    propertySetters[i].SetValue(entity, value);
                }
                entities.Add(entity);
                count++;
                totalCount++;
                if (count == options.BatchSize)
                {
                    action(new FetchResult <T> {
                        Results = entities, Batch = batch
                    });
                    entities.Clear();
                    count = 0;
                    batch++;
                }
            }

            if (entities.Count > 0)
            {
                action(new FetchResult <T> {
                    Results = entities, Batch = batch
                });
            }
            //close the DataReader
            reader.Close();
        }