public static IEnumerable <TModel> As <TModel>(this DataTable table) where TModel : new()
        {
            if (table == null || table.Rows.Count == 0)
            {
                return(new List <TModel>());
            }

            var modelProperties = Activator.CreateInstance <TModel>().GetType().GetProperties();
            var columns         = GetDataTableColumns(table);

            return(table.Rows.Cast <DataRow>().Select(row =>
            {
                var obj = new TModel();

                foreach (var prop in modelProperties)
                {
                    var column = SqlDataMapperHelper.GetColumn(prop);

                    if (columns.Contains(column) && row[column] != DBNull.Value)
                    {
                        prop.SetValue(obj, row[column]);
                    }
                }

                return obj;
            }));
        }
Example #2
0
        public static IEnumerable <TModel> As <TModel>(this DataTable table) where TModel : new()
        {
            if (table == null || table.Rows.Count == 0)
            {
                yield break;
            }

            var modelProperties = Activator.CreateInstance <TModel>().GetType().GetProperties();
            var columns         = GetDataTableColumns(table);

            foreach (DataRow row in table.Rows)
            {
                var obj = new TModel();

                foreach (var prop in modelProperties)
                {
                    var column = SqlDataMapperHelper.GetColumn(prop);

                    if (columns.Contains(column) && row[column] != DBNull.Value)
                    {
                        prop.SetValue(obj, row[column], null);
                    }
                }

                yield return(obj);
            }
        }