• C# DataTable 转实体对象


      DataTable 转实体对象

            /// <summary>
            /// DataTable通过反射获取单个对象
            /// </summary>
            public static T ToSingleModel<T>(this DataTable data) where T : new()
            {
                if (data != null && data.Rows.Count > 0 && data.Rows.Count < 2)
                    return data.GetList<T>(null, true).Single();
                return default(T);
            }
            private static List<T> GetList<T>(this DataTable data, string prefix, bool ignoreCase = true) where T : new()
            {
                List<T> t = new List<T>();
                int columnscount = data.Columns.Count;
                if (ignoreCase)
                {
                    for (int i = 0; i < columnscount; i++)
                        data.Columns[i].ColumnName = data.Columns[i].ColumnName.ToUpper();
                }
                try
                {
                    var properties = new T().GetType().GetProperties();
    
                    var rowscount = data.Rows.Count;
                    for (int i = 0; i < rowscount; i++)
                    {
                        var model = new T();
                        foreach (var p in properties)
                        {
                            var keyName = prefix + p.Name + "";
                            if (ignoreCase)
                                keyName = keyName.ToUpper();
                            for (int j = 0; j < columnscount; j++)
                            {
                                if (data.Columns[j].ColumnName == keyName && data.Rows[i][j] != null)
                                {
                                    string pval = data.Rows[i][j].ToString();
                                    if (!string.IsNullOrEmpty(pval))
                                    {
                                        try
                                        {
                                            if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                                            {
                                                p.SetValue(model, Convert.ChangeType(data.Rows[i][j], p.PropertyType.GetGenericArguments()[0]), null);
                                            }
                                            else
                                            {
                                                p.SetValue(model, Convert.ChangeType(data.Rows[i][j], p.PropertyType), null);
                                            }
                                        }
                                        catch (Exception x)
                                        {
                                            throw x;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        t.Add(model);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return t;
            }
  • 相关阅读:
    java 命令
    测试事件响应修改界面内容
    ASP.NET MVC 解决账号重复登录问题
    Redis 安装
    js返回页面顶部
    Brackets 前端编辑器推荐
    一点点............
    响应式——em,rem,px
    新知识——响应式
    面试心得
  • 原文地址:https://www.cnblogs.com/FGang/p/11264346.html
Copyright © 2020-2023  润新知