• DataTable转List<T>


    static void Main(string[] args)
            {
                DataTable dt = Query();
                Func<DataRow, Usr> func = dr => new Usr { ID = dr.Field<Int32?>("ID"), Name = dr.Field<String>("Name") };
                List<Usr> usrs = new List<Usr>(dt.Rows.Count);
                Stopwatch sw = Stopwatch.StartNew();
                foreach (DataRow dr in dt.Rows)
                {
                    Usr usr = func(dr);
                    usrs.Add(usr);
                }
                sw.Stop();
                Console.WriteLine(sw.ElapsedMilliseconds);
                usrs.Clear();
                Console.ReadKey();
            }
    

      

    public static class EntityConverter
        {
            /// <summary>
            /// DataTable生成实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dataTable"></param>
            /// <returns></returns>
            public static List<T> ToList<T>(this DataTable dataTable) where T : class, new()
            {
                if (dataTable == null || dataTable.Rows.Count <= 0) throw new ArgumentNullException("dataTable", "当前对象为null无法生成表达式树");
                Func<DataRow, T> func = dataTable.Rows[0].ToExpression<T>();
                List<T> collection = new List<T>(dataTable.Rows.Count);
                foreach (DataRow dr in dataTable.Rows)
                {
                    collection.Add(func(dr));
                }
                return collection;
            }
    
            /// <summary>
            /// 生成表达式
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dataRow"></param>
            /// <returns></returns>
            public static Func<DataRow, T> ToExpression<T>(this DataRow dataRow) where T : class, new()
            {
                if (dataRow == null) throw new ArgumentNullException("dataRow", "当前对象为null 无法转换成实体");
                ParameterExpression paramter = Expression.Parameter(typeof(DataRow), "dr");
                List<MemberBinding> binds = new List<MemberBinding>();
                for (int i = 0; i < dataRow.ItemArray.Length; i++)
                {
                    String colName = dataRow.Table.Columns[i].ColumnName;
                    PropertyInfo pInfo = typeof(T).GetProperty(colName);
                    if (pInfo == null) continue;
                    MethodInfo mInfo = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow), typeof(String) }).MakeGenericMethod(pInfo.PropertyType);
                    MethodCallExpression call = Expression.Call(mInfo, paramter, Expression.Constant(colName, typeof(String)));
                    MemberAssignment bind = Expression.Bind(pInfo, call);
                    binds.Add(bind);
                }
                MemberInitExpression init = Expression.MemberInit(Expression.New(typeof(T)), binds.ToArray());
                return Expression.Lambda<Func<DataRow, T>>(init, paramter).Compile();
            }
        }
    

      原创作品,转载请注明出处:http://www.cnblogs.com/lclblog/p/6701814.html

  • 相关阅读:
    JNI内存使用问题(转载)
    typearray和obtainStyledAttribute的作用
    handler looper代码总结(原创)精品推荐
    Appium和Robotium在文字输入上的区别
    老李分享:robotium3.6与4.0 later 的区别 2
    老李分享:robotium3.6与4.0 later 的区别 1
    老李分享:robotium常用API 2
    老李分享:robotium常用API 1
    老李分享:Android -自动化埋点 3
    老李分享:Android -自动化埋点 2
  • 原文地址:https://www.cnblogs.com/Harvard-L/p/6704003.html
Copyright © 2020-2023  润新知