예제 #1
0
        public static DataTable GetDataReader <T>(this DbContext context, IEnumerable <T> entities)
        {
            var propertyMaps = context.Db <T>().Properties;
            var table        = propertyMaps.CreateTable();
            var columns      = propertyMaps.Select(o => new
            {
                o.ColumnName,
                GetValue = o.IsDiscriminator
                    ? x => typeof(T).Name
                    : ExpressHelper.GetPropGetter <T>(o.PropertyName).Compile()
            }).ToArray();

            foreach (var entity in entities)
            {
                var dataRow = table.Rows.Add();
                foreach (var column in columns)
                {
                    dataRow[column.ColumnName] = column.GetValue(entity) ?? DBNull.Value;
                }
            }
            return(table);
        }
예제 #2
0
        private static DataTable GetDataTable <T>(this IContextAdapter contextAdapter, IEnumerable <T> entities)
        {
            var propertyMaps = contextAdapter.GetProperties <T>().ToArray();
            var table        = propertyMaps.CreateTable();
            var columns      = propertyMaps.Select(o => new
            {
                o.ColumnName,
                GetValue = o.IsDiscriminator
                    ? x => contextAdapter.GetDiscriminatorValue(x.GetType())
                    : ExpressHelper.GetPropGetter <T>(o.PropertyName).Compile(),
                o.ValueTransform
            }).ToArray();

            foreach (var entity in entities)
            {
                var dataRow = table.Rows.Add();
                foreach (var column in columns)
                {
                    dataRow[column.ColumnName] = column.ValueTransform(column.GetValue(entity)) ?? DBNull.Value;
                }
            }

            return(table);
        }