zoukankan      html  css  js  c++  java
  • 扩展方法 DataTable的ToList<T>

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace Extension
    {
    
        public static class Extension
        {
            public static IList<T> ToList<T>(this DataTable dt)
            {
                var lst = new List<T>();
                var plist = new List<System.Reflection.PropertyInfo>(typeof(T).GetProperties());
                foreach (DataRow item in dt.Rows)
                {
                    T t = System.Activator.CreateInstance<T>();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
                        if (info != null)
                        {
                            if (!Convert.IsDBNull(item[i]))
                            {
                                info.SetValue(t, item[i], null);
                            }
                        }
                    }
    lst.Add(t); } return lst; ///throw new NotImplementedException(); } } }

      

    DataTable dt = DAL.DAL.gettable();

    var u = dt.ToList<user>();

     http://www.cnblogs.com/jasonxuvip/archive/2012/08/03/2621674.html

  • 相关阅读:
    autocare使用命令
    使用国内豆瓣源
    HCNA(二)以太网的帧结构
    HCNA(一)网络传输介质
    Python
    Python
    Python
    Delphi
    HCNP
    Python
  • 原文地址:https://www.cnblogs.com/xiaochun126/p/5133808.html
Copyright © 2011-2022 走看看